eVolve
eVolve

Reputation: 1456

In .Net Core what happens a request in an api if the caller times out / closes the connection?

I currently have a restful api created using .Net Core. A third party can call my api and some additional calls are made from my api before returning a response.

In the situation that third parties request timeout / they cancel the request, what happens the request within the .net core api. Will it continue to stay alive and return a response or will the thread / call stop?

If it will stop, how can I ensure than this does not happen?

Upvotes: 6

Views: 2841

Answers (1)

alsami
alsami

Reputation: 9815

It depends on your implementation.

You can inject a CancellationToken to each controller endpoint and use it, to check whether a request has been canceled or not.

By default, when the browser cancels a request, that information is received from the server and the cancellation is being requested. The flag RequestAborted is then being set on the HttpContext available in each request or via the IHttpContextAccessor outside of controller endpoints.

Prior ASP.NET Core 2.0 there was a problem when using IIS as a reverse-proxy. It wouldn't recognize the cancellation of a request.

Some resources:

https://odetocode.com/blogs/scott/archive/2018/09/12/cancellationtokens-and-aborted-asp-net-core-requests.aspx

https://andrewlock.net/using-cancellationtokens-in-asp-net-core-mvc-controllers/

https://dev.to/joaofbantunes/using-cancellation-tokens-on-aspnet-core-mvc-actions-57hi

https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.httpcontext.requestaborted?view=aspnetcore-2.2

Upvotes: 7

Related Questions