7

In ASP .NET Core, I can inject a CancellationToken into my actions which will signal cancellation when the HTTP request is cancelled by the browser.

I'm thinking this is detected from the server side by detecting if the underlying TCP connection is closed, but I haven't been able to find anything regarding this anywhere.

Does the browser somehow send a message to the server that indicates cancellation?

Mathias Lykkegaard Lorenzen
  • 15,031
  • 23
  • 100
  • 187
  • Well, since HTTP itself is not bidirectional and while the server computes the request, there is no way to cancel it (without making second - and quite convoluted - request), which is not made, then all is left is TCP connection close, which is easily detected by the server. – AgentFire Jan 13 '20 at 08:00
  • 1
    There are some "Abort Stream" hints [here (kestrel server)](https://github.com/dotnet/aspnetcore/blob/master/src/Servers/Kestrel/Core/src/Internal/Http2/Http2Stream.cs)., may be it is what you're looking for. If not, try some other files. – AgentFire Jan 13 '20 at 08:09
  • https://stackoverflow.com/questions/31061838/how-do-i-cancel-an-http-fetch-request – Kiksen Jan 13 '20 at 13:45
  • 1
    https://www.oreilly.com/library/view/http-the-definitive/1565925092/ch04s07.html – Kiksen Jan 13 '20 at 13:47

1 Answers1

6

The cancellation token is triggered when the connection is closed. The browser doesn't communicate anything. It's the action of the user browsing away, closing the tab/window, etc.: situations where the browser responds by closing the connection to the server.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • And what do you think of AbortController? Does that send a request then? And is it supported by ASP .NET Core? https://developer.mozilla.org/en-US/docs/Web/API/AbortController – Mathias Lykkegaard Lorenzen Jan 13 '20 at 19:38
  • That's entirely a JS thing. It has no relationship to anything going on in ASP.NET Core. *If* aborting causes the underlying connection to be closed, then it should also trigger cancellation server-side, but it's not clear from the spec that it does. – Chris Pratt Jan 13 '20 at 20:04