CancellationToken in controller endpoint hosted in Azure isn't cancelled when the request is cancelled.

Basaingeal 11 Reputation points
2021-05-11T18:48:46.927+00:00

It appears that when a .NET Web API is hosted in Azure, and the controller endpoints take in a CancellationToken, that when the request to the API is cancelled, that CancellationToken is not cancelled.

This is clarified by Azure support in this GitHub issue: https://github.com/dotnet/aspnetcore/issues/20229#issuecomment-623289191

It is also expected that CancellationToken feature is not applicable to App Service.

This doesn't seem to be expected behavior to me. It isn't documented anywhere that I can find, and it's a stark difference from what occurs during local development. It looks like when the browser refreshes that any running requests will have their CancellationTokens cancelled. But then in Azure, they silently never cancel.

This is a performance concern when running large queries on behalf of the API. We would want the query to cancel if the user cancelled their request, as it's using up machine resources, and that request is never going to complete because the browser cancelled it.

Is there any way to get Azure Web Apps to detect when a request has been cancelled? And if not, could documentation be added in order to clarify that CancellationTokens don't work in Azure?

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
6,902 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ryan Hill 25,826 Reputation points Microsoft Employee
    2021-05-13T13:31:55.407+00:00

    @Basaingeal , I do believe it is still the case that the front-end doesn't forward connection closed events to the web worker. I will verify with our product teams. Once verified, I'll work with our content authors to determine where best to place this messaging.

    One way to get around this is implementing a pub/sub pattern or making use of web sockets.