dotnet / dotnet/aspnetcore

HttpContext.RequestAborted is not canceled when app is hosted under IIS with hostingModel = OutOfProcess

Open
#54,936 2 comments 1 reaction 0 assignees View on GitHub
area-networking feature-iis
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

Cancellation token `HttpContext.RequestAborted` is not triggered when App is hosted in IIS with such configuration:

```xml








```

Method example:
```csharp
private readonly ILogger _logger;

[HttpGet(Name = "GetWeatherForecast")]
public async Task> Get()
{
_logger.Log(LogLevel.Information, "Got New Weather Request");
this.HttpContext.RequestAborted.Register(() =>
{
_logger.Log(LogLevel.Information,$"Request aborted");
});

await Task.Delay(TimeSpan.FromSeconds(10), this.HttpContext.RequestAborted);
// return logic here
}
```

Issue that seems related:
* https://github.com/aspnet/AspNetCoreModule/issues/38

### Expected Behavior

Cancellation token `HttpContext.RequestAborted` should trigger when a user cancels http request to IIS when app is hosted with AspNetCoreModuleV2 with hostingModel = OutOfProcess

### Steps To Reproduce

#### Case1 - cancelation **doesn't work in IIS**

1. Clone repo: https://github.com/Roganik/SlowWeatherInIIS
2. Publish the app with `dotnet publish -c Release`
3. Host this app in IIS (you might need to install .NET Core Hosting Bundle).
3.1 In my case I created the new website pointing to the publish output folder `C:\Projects\SlowWeatherInIIS\bin\Release\net8.0\publish`
3.2 And I bind it to the 8081 port
4. Open the url in browser
4.1 Open http://localhost:8081/WeatherForecast and wait 10 seconds for output
4.2 Open http://localhost:8081/WeatherForecast again, but cancel request before it finishes (just close browser tab)
5. Check logs in logs folder `C:\Projects\SlowWeatherInIIS\bin\Release\net8.0\publish\logs`

Actual: Has two "Got New Weather Request" logs.
Expected: Has two "Got New Weather Request" logs and has one "Request aborted" log.

_The "Request aborted" log is missing._

#### Case2 - cancelation works as expected when hosted by kerstel only
1. Clone repo: https://github.com/Roganik/SlowWeatherInIIS
2. Publish the app with `dotnet publish -c Release`
3. Start the app
3.1 Navigate to publish folder (in my case `C:\Projects\SlowWeatherInIIS\bin\Release\net8.0\publish` )
3.2 Start web service in cmd: `dotnet .\SlowWeatherInIIS.dll`
3.3 Read the base url of app from console. (in my case http://localhost:5000 )
4. Open the url in browser.
4.1 Open http://localhost:5000/WeatherForecast and wait 10 seconds for output
4.2 Open http://localhost:5000/WeatherForecast again but cancel request before it finishes (just close browser tab)
5. Check logs in cmd

Actual: Has two "Got New Weather Request" logs and has one "Request aborted" log
Expected: Actual behavior matches expected.

### Exceptions (if any)

_No response_

### .NET Version

8.0.103

### Anything else?

Reproduced this behavior locally with:
* Microsoft .Net 8.0.3 - Windows Server Hosting
* IIS Version 10.0.22621.1
* Windows: 23H2 (OS Build 22631.3235)

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue with the linked SlowWeatherInIIS app and compare IIS OutOfProcess hosting with direct Kestrel hosting. Read the HttpContext.RequestAborted usage and the AspNetCoreModuleV2 hosting configuration, then verify that canceling the IIS request triggers the registered callback and the cancellation-aware delay.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.