Skip to content
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

Closed
Tracked by #28596
adityamandaleeka opened this issue Apr 10, 2023 · 21 comments · Fixed by #29068
Closed
Tracked by #28596

Add docs for request timeouts middleware #28934

adityamandaleeka opened this issue Apr 10, 2023 · 21 comments · Fixed by #29068
Assignees
Labels
8.0 .NET 8 doc-idea seQUESTered Identifies that an issue has been imported into Quest.

Comments

@adityamandaleeka
Copy link
Member

adityamandaleeka commented Apr 10, 2023

.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:

builder.Services.AddRequestTimeouts();

You can then apply request timeouts using middleware by calling UseRequestTimeouts():

app.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

@adityamandaleeka
Copy link
Member Author

cc @Tratcher

@tdykstra
Copy link
Contributor

@Kahbazi do you have a sample app or code snippets that show how to use the timeout middleware?

@github-actions github-actions bot added seQUESTered Identifies that an issue has been imported into Quest. and removed reQUEST Triggers an issue to be imported into Quest labels Apr 18, 2023
@dodyg
Copy link
Contributor

dodyg commented Apr 19, 2023

@tdykstra
Copy link
Contributor

tdykstra commented Apr 19, 2023

@dodyg Thanks. Can you also show code that uses the TimeoutStatusCode and WriteTimeoutResponse properties of Microsoft.AspNetCore.Http.Timeouts.RequestTimeoutPolicy?

Edit -- Please put any sample code you want to contribute in the same folder as this stub.txt file

@tdykstra
Copy link
Contributor

@dodyg Thanks.

@prinsalvino
Copy link

prinsalvino commented Sep 21, 2023

Hi @dodyg. could you please show the code to use the RequestTimeout attribute?
already registered the service using builder.Services.AddRequestTimeouts() and app.UseRequestTimeouts(). but when I try to use the attribute in the controller, it doesn't seems to work

 public class CountryController : ControllerBase
 {
       [HttpGet]
       [RequestTimeout(milliseconds: 1)]
       public override IActionResult GetList()
       {
            return Ok(MdlCountry.GetList());
       }
}

@dodyg
Copy link
Contributor

dodyg commented Sep 21, 2023

@tdykstra
Copy link
Contributor

@prinsalvino Are you testing in debug mode? You won't see any timeouts when testing in debug mode.

@prinsalvino
Copy link

@tdykstra I have tried doing it in release mode but problem is still the same. I use docker to run my application

@Kahbazi
Copy link
Member

Kahbazi commented Sep 23, 2023

@prinsalvino The timeout middleware only sets the HttpContext.RequestAborted. You have to check it yourself in theapplication code and decide if you want to throw if HttpContext.RequestAborted is cancelled.

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());
    }
}

@prinsalvino
Copy link

prinsalvino commented Sep 25, 2023

@Kahbazi I copy pasted the code. run both in debug and release mode but still not successful. tried to debug the request aborted value
▶ | HttpContext.RequestAborted | IsCancellationRequested = false | System.Threading.CancellationToken

Checked the services as well and it is registered

@dodyg
Copy link
Contributor

dodyg commented Sep 25, 2023

@dodyg
Copy link
Contributor

dodyg commented Sep 25, 2023

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 RequestTimeout although you can do so with minimal API extension method .WithRequestTimeout(policy: null);

@tdykstra
Copy link
Contributor

I can't figure out how to use default policy using RequestTimeout although you can do so with minimal API extension method .WithRequestTimeout(policy: null);

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

@dodyg
Copy link
Contributor

dodyg commented Sep 26, 2023

Thanks. Somehow I ended up with a wrong mental model on how the default timeout policy should work.

@prinsalvino
Copy link

@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
Copy link
Contributor

dodyg commented Sep 27, 2023

@prinsalvino

image

@prinsalvino
Copy link

prinsalvino commented Sep 29, 2023

@dodyg I have no idea what's going on here
image

@dodyg
Copy link
Contributor

dodyg commented Sep 30, 2023

@prinsalvino
Copy link

prinsalvino commented Oct 2, 2023

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
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
8.0 .NET 8 doc-idea seQUESTered Identifies that an issue has been imported into Quest.
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

7 participants