<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/using-request-timeouts-middleware-in-web-api-ijbjckoyi" -->

---
title: Using request timeouts middleware in web API | daily.dev
description: A revisit of an earlier approach to limiting a REST API endpoint's response time while allowing processing to continue in the background. The original...
canonical: https://daily.dev/posts/using-request-timeouts-middleware-in-web-api-ijbjckoyi
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Using request timeouts middleware in web API | daily.dev
og:description: A revisit of an earlier approach to limiting a REST API endpoint's response time while allowing processing to continue in the background. The original...
og:url: https://daily.dev/posts/using-request-timeouts-middleware-in-web-api-ijbjckoyi
og:image: https://api.daily.dev/og/posts/ijbJcKOyI.png
og:image:alt: Using request timeouts middleware in web API
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Using request timeouts middleware in web API

**[Damir's Corner](https://daily.dev/sources/damirscorner)** · 3 min read · 5 upvotes · 1 comments

## Summary

A revisit of an earlier approach to limiting a REST API endpoint's response time while allowing processing to continue in the background. The original hand-rolled solution used a CancellationTokenSource plus a parallel Task.Delay to enforce a timeout. This version replaces that with ASP.NET Core's built-in request timeouts middleware (available since .NET 8), hooking into HttpContext.RequestAborted and using Task.WaitAsync with the request's cancellation token, removing the need for a separately configured delay value. Includes setup code (AddRequestTimeouts, UseRequestTimeouts, RequestTimeout attribute) and a link to a sample GitHub repository with tests covering completion, abort, and background-continuation scenarios.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.damirscorner.com/blog/posts/20261002-UsingRequestTimeoutsMiddlewareInWebApi.html>

## Questions this post answers

### How do I use WaitAsync with a cancellation token to let a background task continue after a request timeout in ASP.NET Core?

Wrap the long-running method call with .WaitAsync(cancellationToken) and catch the resulting OperationCanceledException when cancellationToken.IsCancellationRequested is true. The awaited task completes either when the original work finishes or when the token signals cancellation, whichever comes first, letting the original task keep running in the background while the controller still returns a response using the submission ID already created.

_See a worked ASP.NET Core example of WaitAsync-based timeout handling curated on daily.dev._

### How do I configure the request timeouts middleware in ASP.NET Core so it cancels HttpContext.RequestAborted after a set duration?

Register it with builder.Services.AddRequestTimeouts() and app.UseRequestTimeouts(), then set a duration per action using the [RequestTimeout(milliseconds)] attribute, at the controller level, or as a global default. The middleware hooks into HttpContext.RequestAborted so that token is canceled both when the connection aborts and when the configured timeout expires, removing the need for a manually created CancellationTokenSource.

_Developers wiring up API timeouts can find setup walkthroughs like this on daily.dev._

## Community discussion

Top comments from developers on daily.dev.

**@pdfopsdev** · 0 upvotes

> ran into exactly this with a hand-rolled CancellationTokenSource setup before, forgot to dispose the linked token and leaked tasks under load. glad this finally shipped as built-in middleware instead of everyone rolling their own.

## Similar posts on daily.dev

- [Request Timeouts in ASP.NET Core](https://daily.dev/posts/request-timeouts-in-asp-net-core-p1pfhzrz4) · Milan Jovanović · 17 upvotes · 1 comments
- [ASP.NET Core Middleware: Building and Using the Request Pipeline](https://daily.dev/posts/asp-net-core-middleware-building-and-using-the-request-pipeline-tav1b2ck7) · We Are .NET · 1 upvotes · 0 comments
- [Mastering CancellationTokens in C\# and ASP.NET Core](https://daily.dev/posts/mastering-cancellationtokens-in-c-and-asp-net-core-ek9w4rchj) · ITNEXT · 23 upvotes · 0 comments
- [HttpClient Resilience in .NET 10: Timeout, Retry, and Circuit Breaker with Microsoft.Extensions.Http.Resilience](https://daily.dev/posts/httpclient-resilience-in-net-10-timeout-retry-and-circuit-breaker-with-microsoft-extensions-http-aszonhwdy) · We Are .NET · 5 upvotes · 0 comments

---

Tags: [#c#](https://daily.dev/tags/c#), [#aspnet](https://daily.dev/tags/aspnet)

[View this post on daily.dev](https://daily.dev/posts/using-request-timeouts-middleware-in-web-api-ijbjckoyi)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Using request timeouts middleware in web API","url":"https://daily.dev/posts/using-request-timeouts-middleware-in-web-api-ijbjckoyi","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/using-request-timeouts-middleware-in-web-api-ijbjckoyi"},"datePublished":"2026-10-02T06:28:22.077Z","dateModified":"2026-10-02T06:28:43.839Z","description":"A revisit of an earlier approach to limiting a REST API endpoint's response time while allowing processing to continue in the background. The original...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/425073b57495acf9675afac1769c56ee?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/425073b57495acf9675afac1769c56ee?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Damir's Corner","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Damir's Corner","logo":"https://media.daily.dev/image/upload/s--9Wkl5ofQ--/f_auto,q_auto/v1780213496/logos/damirscorner?_a=BAMAMiWQ0","url":"https://daily.dev/sources/damirscorner"},"commentCount":1,"discussionUrl":"https://daily.dev/posts/using-request-timeouts-middleware-in-web-api-ijbjckoyi","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":5},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":1}],"keywords":"c#,aspnet","timeRequired":"PT3M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Damir's Corner","item":"https://daily.dev/sources/damirscorner"},{"@type":"ListItem","position":3,"name":"Using request timeouts middleware in web API"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/using-request-timeouts-middleware-in-web-api-ijbjckoyi","comment":[{"@type":"Comment","text":"ran into exactly this with a hand-rolled CancellationTokenSource setup before, forgot to dispose the linked token and leaked tasks under load. glad this finally shipped as built-in middleware instead of everyone rolling their own.","datePublished":"2026-10-02T18:20:12.403Z","url":"https://daily.dev/posts/ijbJcKOyI#c-cQ6YWWcjE","author":{"@type":"Person","name":"PDFops","url":"https://daily.dev/pdfopsdev","image":"https://media.daily.dev/image/upload/s---8isRBKc--/f_auto/v1782922291/avatars/avatar_orjMeK8QKaaVZwGq7ScPz?_a=BAMAMicg0"}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/using-request-timeouts-middleware-in-web-api-ijbjckoyi#faq","mainEntity":[{"@type":"Question","name":"How do I use WaitAsync with a cancellation token to let a background task continue after a request timeout in ASP.NET Core?","acceptedAnswer":{"@type":"Answer","text":"Wrap the long-running method call with .WaitAsync(cancellationToken) and catch the resulting OperationCanceledException when cancellationToken.IsCancellationRequested is true. The awaited task completes either when the original work finishes or when the token signals cancellation, whichever comes first, letting the original task keep running in the background while the controller still returns a response using the submission ID already created. See a worked ASP.NET Core example of WaitAsync-based timeout handling curated on daily.dev."}},{"@type":"Question","name":"How do I configure the request timeouts middleware in ASP.NET Core so it cancels HttpContext.RequestAborted after a set duration?","acceptedAnswer":{"@type":"Answer","text":"Register it with builder.Services.AddRequestTimeouts() and app.UseRequestTimeouts(), then set a duration per action using the [RequestTimeout(milliseconds)] attribute, at the controller level, or as a global default. The middleware hooks into HttpContext.RequestAborted so that token is canceled both when the connection aborts and when the configured timeout expires, removing the need for a manually created CancellationTokenSource. Developers wiring up API timeouts can find setup walkthroughs like this on daily.dev."}}]}
```

