1

Is there another http request that has to be issued?

How does the browser notify the server of abandoned/cancelled request?

Do I need to code my client-side for that to work?

This is my ASP MVC Controller method:

[HttpGet("/get")]
public async Task<string> Get(CancellationToken cancellationToken)
{
   //propagate CancellationToken to Entity Framework

}
Dan
  • 11,077
  • 20
  • 84
  • 119
  • Can you provide a simple code example? The `CancellationToken` is very common and the exact implementation details depends on the actual function you are calling. – Stefan Feb 07 '18 at 22:06

1 Answers1

4
  1. Browsers can cancel HTTP requests by closing the underlying TCP connection. HTTP.SYS and IIS detect this and pass the connection-closed notification to application code such as ASP.NET. In ASP.NET this is exposed through the HttpResponse.IsClientConnected property and other places. See this QA: Can a http server detect that a client has cancelled their request?
  2. Webpage client scripts cannot cancel a "main" request (a user manually navigating by using the address bar or clicking a link in a page) - only users can cancel that by pressing the Stop button. Your client code can cancel XMLHttpRequest (AJAX) and modern fetch requests by using the abort() function (jQuery also exposes this in their own $.ajax return object).
  3. This article explains how to use CancellationToken inside async Controller Action methods: https://andrewlock.net/using-cancellationtokens-in-asp-net-core-mvc-controllers/
  4. Entity Framework 6 has limited support for async operations, mostly through ToListAsync for queries and SaveChangesAsync for store operations. Make sure you're using the right extension-methods: QueryableExtensions.ToListAsync does not accept a CancellationToken but DbRawSqlQuery.ToListAsync does. It should be apparent that async is incompatible with EF's Lazy-Loading feature, so you'll need to preemptively load all needed entities in async code so lazy-loading won't be invoked. I recommend disabling Lazy-Loading in most cases anyway.
Dai
  • 141,631
  • 28
  • 261
  • 374