Skip to content

Cancellationtoken not Triggering when using IIS #20229

@pawod

Description

@pawod

I'm working on a ASP.NET Core web application running on regular net461 (also tried net48), where I defined an HTTP endpoint like this:

[HttpGet]
[Route("query/{queryName}")]
public async Task<IActionResult> Call(string queryName, CancellationToken cancellationToken)
{
  await Task.Delay(5_000, cancellationToken);
  ...
}

As the title already states, I can't make the cancellationToken trigger a TaskCanceledException upon initiating a refresh on browser side, while the initial request is still being processed (Chrome devtools displays these events as cancelled requests). The only way I can make it work, is by selecting "Project" in Visual Studio's Debug settings (see here), which I guess, makes the whole application run in Kestrel only - without any reverse proxy in front.

I came across various posts on SO (like this one) and GitHub (like this one) that claimed this issue would be solved with ASP.NET Core 2.2, where in-process hosting should work in combination with CancellationToken.

So I updated all my nuget packages, but unfortunately with no effect, the error still persists. Yes, locally I'm debugging using IIS Express and I'm not sure how exactly this differs from a regular IIS setup, but I'm also running this app on Azure, where the whole process looks like it was running in-process, because there is only a single process running (see here).

This is an extract from my csproj with the AspNetCore packages I installed:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.13.1" />
    <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Server.IIS" Version="2.2.6" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.2.0" />
    <PackageReference Include="System.Net.Http" Version="4.3.4" />
  </ItemGroup>
</Project>

I also played around with the Program.cs, because I suspect this might be another potential source of error. I Tried to use the UseIIS() extension method only to enforce in-process hosting, but also no visible effect with regard to CancellationToken:

public class Program
{
    public static void Main(string[] args)
    {
        var host = WebHost.CreateDefaultBuilder(args)
            //.UseKestrel()
            .UseIIS()
            .UseContentRoot(Directory.GetCurrentDirectory())
            //.UseIISIntegration()
            .UseStartup<Startup>()
            .UseApplicationInsights()
            .Build();

        host.Run();
    }
}

What am I doing wrong? Does CancellationToken only work with netcoreapp2.x (or higher) as target framework? Might this be a configuration issue (Program.cs, Startup.cs, csproj, etc)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractionsfeature-iisIncludes: IIS, ANCM

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions