I have the following code snippet in an endpoint in a .NET 6 API, where the request is in a loop until a viable condition is found to return:
while (true)
{
try
{
cancellationToken.ThrowIfCancellationRequested();
}
catch
{
_unitOfWork.ResetContextState();
if (cancellationToken.IsCancellationRequested)
{
}
}
}
The cancellationToken variable is an object of type CancellationToken from the System.Threading package and I receive the object directly in the API controller. The object is sent and controlled directly by .NET.
When running on localhost, if I cancel the request, the code enters the catch block and then the if statement (cancellationToken.IsCancellationRequested
). However, when the API is hosted on Azure App Service Linux, the same flow is not respected when cancelling the request instead, the request is abruptly terminated. Does anyone know why this happens?
I would guess the difference is that on Azure App Service your app is behind a load balancer running IIS on windows, not really exposed, and it looks like that the event is not forwarded:
Cancellationtoken not Triggering when using IIS
That github issue is old though, there could be something configurable somehow? Like this , totally different problem but still modifying Azure App Service default behaviour in relation to the Azure load balancers.
Seems the IIS/Windows part is not up to date anymore: https://devblogs.microsoft.com/dotnet/bringing-kestrel-and-yarp-to-azure-app-services/