5.9K Star 11.7K Fork 4K

GVPdotNET China / Furion

 / 详情

sse方式的接口报错 System.InvalidOperationException: Headers are read-only, response has already started.

Done
Opened this issue  
2023-11-22 20:46

版本号

4.9.1.5 (最新)

.NET SDK

.NET6

项目类型

WebApi

操作系统

Windows 10

运行环境

开发环境 (Development)

数据库类型

Sqlite

应用托管程序

单文件

这个问题是否已经存在?

如何复现

1.用脚手架创建项目MyProject。
2.然后在MyProject.Web.Entry新建一个Api

 [HttpPost("api/sse"), AllowAnonymous]
 public async Task CreateSseDemo()
 {
     // 设置响应头,指定 SSE 的内容类型
     HttpContext.Response.Headers.Add("Content-Type", "text/event-stream");

     // 写入 SSE 消息到响应流
     for (int i = 0; i < 10; i++)
     {
         var message = $"消息{i}";
         await HttpContext.Response.WriteAsync(message);
         await HttpContext.Response.Body.FlushAsync();
         await Console.Out.WriteLineAsync(message);
         Task.Delay(1000).Wait();
     }
     await HttpContext.Response.CompleteAsync();
 }

预期结果

不要报错,虽然报错信息对功能没有影响。

实际结果

消息0
消息1
消息2
消息3
消息4
消息5
消息6
消息7
消息8
消息9
fail: 2023-11-22 20:29:21.6093482 +08:00 星期三 L Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] #9:🤡 [新功能] 应用全局配置
An unhandled exception has occurred while executing the request.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
System.InvalidOperationException: Headers are read-only, response has already started.
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.ThrowHeadersReadOnlyException()
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseHeaders.Microsoft.AspNetCore.Http.IHeader
Dictionary.set_ContentType(StringValues value)
at Microsoft.AspNetCore.Http.DefaultHttpResponse.set_ContentType(String value)
at Microsoft.AspNetCore.Mvc.Infrastructure.SystemTextJsonResultExecutor.ExecuteAsync(ActionContext context, Jso
nResult result)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|22_0(ResourceInvoker in
voker, IActionResult result)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|30_0[TFilter
,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope&
scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|20_0(ResourceI
nvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|17_1(ResourceInvoker invoker)

     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)

     at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requ

estTask, ILogger logger)
at StackExchange.Profiling.MiniProfilerMiddleware.Invoke(HttpContext context) in C:\projects\dotnet\src\MiniPro
filer.AspNetCore\MiniProfilerMiddleware.cs:line 112
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerPro
vider)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
warn: 2023-11-22 20:29:21.7435143 +08:00 星期三 L Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[2] #9:🤡 [新功能] 应用全局配置
The response has already started, the error page middleware will not be executed.
fail: 2023-11-22 20:29:21.7528209 +08:00 星期三 L Microsoft.AspNetCore.Server.Kestrel[13] #9:🤡 [新功能] 应用全局配置
Connection id "0HMVBAQA355SL", Request id "0HMVBAQA355SL:00000002": An unhandled exception was thrown by the appli
cation.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
System.InvalidOperationException: Headers are read-only, response has already started.
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.ThrowHeadersReadOnlyException()
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseHeaders.Microsoft.AspNetCore.Http.IHeader
Dictionary.set_ContentType(StringValues value)
at Microsoft.AspNetCore.Http.DefaultHttpResponse.set_ContentType(String value)
at Microsoft.AspNetCore.Mvc.Infrastructure.SystemTextJsonResultExecutor.ExecuteAsync(ActionContext context, Jso
nResult result)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|22_0(ResourceInvoker in
voker, IActionResult result)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|30_0[TFilter
,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope&
scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|20_0(ResourceI
nvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|17_1(ResourceInvoker invoker)

     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)

     at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requ

estTask, ILogger logger)
at StackExchange.Profiling.MiniProfilerMiddleware.Invoke(HttpContext context) in C:\projects\dotnet\src\MiniPro
filer.AspNetCore\MiniProfilerMiddleware.cs:line 112
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerPro
vider)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
at Furion.StartupFilter.<>c__DisplayClass0_1.<b__1>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.WebTools.BrowserLink.Net.BrowserLinkMiddleware.InvokeAsync(HttpContext context)
at Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserRefreshMiddleware.InvokeAsync(HttpContext context)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplicati
on`1 application)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++

异常信息

截图或视频

输入图片说明

Demo 地址(Git/网盘/SVN)

https://pan.baidu.com/s/1lATSaK43KzWEnhVHaJG4sQ?pwd=jn8i

友好承诺

  • 我承诺将本着相互尊重、理解和友善的态度进行交流,共同维护好 Furion 来之不易的良好的社区氛围。

Comments (8)

怪兽 created任务

直接创建webapi,不引用框架是没问题的。
输入图片说明

代码还没看,但是你 SSE 都用错了。另外麻烦以后提供 Git 下载地址,而非网盘,一个 12M 的文件百度网盘要下载 5 分钟。

[HttpPost("api/sse"), AllowAnonymous]

这里很大概率是规范化结果影响了。所以再添加一个 NonUnity 特性,如:[HttpPost("api/sse"), NonUnity, AllowAnonymous]

确实是规范化结果影响的,加了NonUnity ,可以了。 :+1: 多谢。

我等下更新一个版本,内部自动处理这些文件、视频、图片、sse等类型,不用手动贴。

不过你倒提醒了我,ChatGPT 带火了 SSE,框架底层应该自动处理这样的 Content-Type

等下我发布一个新版本,以后可以不用贴 [NonUnify] 了,针对一些特定 Content-Type 自动处理了:

3a79c05

百小僧 changed issue state from 待办的 to 已完成

Furion v4.9.1.6 版本已经发布,已经自动处理了,无需贴 [NonUnify] 特性了。

Sign in to comment

Status
Assignees
Milestones
Pull Requests
Successfully merging a pull request will close this issue.
Branches
Planed to start   -   Planed to end
-
Top level
Priority
参与者(2)
974299 monksoul 1578937227
C#
1
https://gitee.com/dotnetchina/Furion.git
git@gitee.com:dotnetchina/Furion.git
dotnetchina
Furion
Furion

Search