dotnet / dotnet/aspnetcore

[dotnet-sdk-7.0.100-rc.1.22413.1] Setting the header Content-Length is not allowed on responses with status code 204 Error in ASP.NET Core apps

Open
#43,316 7 comments 0 reactions 1 assignee Assigned to @adityamandaleeka View on GitHub
area-networking bug
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

During our 3rd Party App Compatibility testing, some cases failed because of this error (StatusCode cannot be set because the response has already started.), We have seen this issue in 4 web applications, we think this is related to Kestrel, issue does not repro when we launch the app with IISExpress via VS.

1) It is related to "C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\7.0.0-rc.1.22412.2\Microsoft.AspNetCore.Server.Kestrel.Core.dll"
2) It might be caused by this PR https://github.com/dotnet/aspnetcore/pull/43103

**Application Name**: OqtaneFramework(3.0.3), QuickApp, EventStore,mixcore
**OS**: Windows 10 RS5
**CPU**: X64
**.NET Build Number:** dotnet-sdk-7.0.100-rc.1.22413.1
**Github Link**: https://github.com/oqtane/oqtane.framework/releases/tag/v3.0.3
**App checking at** : https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1593251/

**Verify Scenarios:**
1). Windows10 RS5 x64+dotnet-sdk-7.0.100-rc.1.22413.1: Fail
2). Windows10 RS5 x64+dotnet-sdk-7.0.100-rc.1.22408.5: Pass
3). Windows10 RS5 x64+dotnet-sdk-6.0.303-win-x64: Pass

### Expected Behavior

It should save the new user

### Steps To Reproduce

**The machine only has 7.0 installed. And DOTNET_ROLL_FORWARD_ON_NO_CANDIDATE_FX=2 is set.**

1. Launch Oqtane.Server.exe
2. Go to https://localhost:5001/
3. Fill required info: username,password, email (any data is OK, this step will create database)
4. Login to app with created username => https://localhost:5001/login
5. Go to https://localhost:5001/admin/users
6. Click Add User
7. Fill information and Save

### Exceptions (if any)

Save Failed
![image](https://user-images.githubusercontent.com/83067197/184843605-2676ad99-87ea-424a-b31b-8c602b6d5871.png)
**Exception:**
```
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
Executed endpoint 'Oqtane.Controllers.UserController.Get (Oqtane.Server)'
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HMJVC2GF9M66", Request id "0HMJVC2GF9M66:00000001": An unhandled exception was thrown by the application.
System.InvalidOperationException: Setting the header Content-Length is not allowed on responses with status code 204.
warn: Microsoft.AspNetCore.Server.Kestrel[0]
Connection processing ended abnormally.
System.InvalidOperationException: StatusCode cannot be set because the response has already started.
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ThrowResponseAlreadyStartedException(String value)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.set_StatusCode(Int32 value)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.SetErrorResponseHeaders(Int32 statusCode)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.CreateResponseHeaders(Boolean appCompleted)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProduceEnd()
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequestsAsync[TContext](IHttpApplication`1 application)
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/1.1 POST https://localhost:5001/api/Log application/json;+charset=utf-8 -
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]

```

### .NET Version

7.0.100-rc.1.22413.1

### Anything else?

```
C:\h\w\AE710988\p>dotnet --info
.NET SDK:
Version: 7.0.100-rc.1.22413.1
Commit: fd587269d0

Runtime Environment:
OS Name: Windows
OS Version: 10.0.19043
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\7.0.100-rc.1.22413.1\

Host:
Version: 7.0.0-rc.1.22411.12
Architecture: x64
Commit: f52d8c59bb

.NET SDKs installed:
7.0.100-rc.1.22413.1 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 7.0.0-rc.1.22412.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 7.0.0-rc.1.22411.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 7.0.0-rc.1.22379.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

```
@dotnet-actwx-bot @dotnet/compat

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.