I'm doing some research on Cancelling Async Requests through WebAPI,
Recently I discovered that latest version of WebApi(not .NET-Core's), supports cancellation tokens. As a test I've written this code.
[HttpGet]
[Route("LongRequest")]
public async Task<string> VeryLongRequest(string Key, CancellationToken token)
{
for (int i = 0; i < 5; ++i)
{
if (false == token.IsCancellationRequested)
{
Thread.Sleep(5000);
}
else
{
Trace.WriteLine("Cancelled");
}
}
return "Complete";
}
This code works, and the cancellation token is updated, when a user closes the window/refreshes/ or navigates to a new page (Not on an SPA).
Being that HTTP Request's are suppose to be stateless, How is the cancellation mapping back to the request?
And how can I Manually Invoke a cancellation from my client?