Is there a way to figure out in ASP.NET Web API beta whether the HTTP request was cancelled (aborted by user of for any another reason)? I'm looking for opportunity to have a kind of cancellation token out-of-the-box that will signal that the request is aborted and therefore long-running ops should be aborted as well.
Possible related question - the use case for the CancellationTokenModelBinder class. What's the reason to have a separate binder for cancellation token?
If you added CancellationToken in to controller methods, it will be automatically injected by the framework, and when a client calls xhr.abort() the token will be automatically cancelled
Something similar to
public Task<string> Get(CancellationToken cancellationToken = default(CancellationToken))
For MVC you can also refer to
HttpContext.Current.Response.IsClientConnected
HttpContext.Response.ClientDisconnectedToken
For .NetCore
services.AddTransient<ICustomInterface>(provider => {
var accessor = provider.GetService<IHttpContextAccessor>);
accessor.HttpContext.RequestAborted;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With