-
Notifications
You must be signed in to change notification settings - Fork 25.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add docs for request timeouts middleware #28934
Comments
cc @Tratcher |
@Kahbazi do you have a sample app or code snippets that show how to use the timeout middleware? |
@dodyg Thanks. Can you also show code that uses the Edit -- Please put any sample code you want to contribute in the same folder as this stub.txt file |
@dodyg Thanks. |
Hi @dodyg. could you please show the code to use the RequestTimeout attribute?
|
@prinsalvino Are you testing in debug mode? You won't see any timeouts when testing in debug mode. |
@tdykstra I have tried doing it in release mode but problem is still the same. I use docker to run my application |
@prinsalvino The timeout middleware only sets the public class CountryController : ControllerBase
{
[HttpGet]
[RequestTimeout(milliseconds: 1)]
public override async Task<IActionResult> GetList()
{
await Task.Delay(2);
HttpContext.RequestAborted.ThrowIfCancellationRequested();
return Ok(MdlCountry.GetList());
}
} |
@Kahbazi I copy pasted the code. run both in debug and release mode but still not successful. tried to debug the request aborted value Checked the services as well and it is registered |
This one shows how to use a named policy https://github.com/dodyg/practical-aspnetcore/tree/net6.0/projects/.net8/request-timeout-5. I can't figure out how to use default policy using |
The Request timeouts doc has a section that shows an example of setting and using a default timeout. https://learn.microsoft.com/en-us/aspnet/core/performance/timeouts?view=aspnetcore-8.0#set-global-default-timeout-policy |
Thanks. Somehow I ended up with a wrong mental model on how the default timeout policy should work. |
@dodyg I copy pasted this sample https://github.com/dodyg/practical-aspnetcore/blob/net6.0/projects/.net8/request-timeout-4/Program.cs. Also added builder.Services.AddRequestTimeouts(); because it throws an exception. Run it and test using swagger. Still got the response "Hello World!" |
@dodyg I have no idea what's going on here |
I see. in my previous screenshot, I run it in release mode via visual studio. Now, I run dotnet publish and dotnet applicationName.dll via cli and it seems to be working. Thank you! @dodyg |
.NET 8 has a new request timeouts middleware, which allows users to set request timeouts for individual endpoints, controllers, or dynamically per request. We should document it.
Some notes that may be useful...
To apply request timeouts, first add the request timeout services:
You can then apply request timeouts using middleware by calling
UseRequestTimeouts()
:To apply request timeouts to a particular endpoint call
WithRequestTimeout(timeout)
or add[RequestTimeout(timeout)]
to the controller or action.The timeouts are cooperative; when they expire, the
HttpContext.RequestAborted
cancellation token will trigger but the request will not be forcibly aborted. It's up to the application to monitor the token and decide how to end the request processing.More info at:
EDIT by @Rick-Anderson to add the following metadata
Document Details
⚠ Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.
Associated WorkItem - 85532
The text was updated successfully, but these errors were encountered: