Reputation: 1456
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
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://andrewlock.net/using-cancellationtokens-in-asp-net-core-mvc-controllers/
https://dev.to/joaofbantunes/using-cancellation-tokens-on-aspnet-core-mvc-actions-57hi
Upvotes: 7