代码之家  ›  专栏  ›  技术社区  ›  Mr.Wang from Next Door

ServiceStack:InvalidOperationException:StatusCode无法设置,因为响应已启动

  •  2
  • Mr.Wang from Next Door  · 技术社区  · 6 年前

    我们的API工作正常,最近我们从 ServiceStack 4.5.14 ServiceStack.Core 1.0.44 使用强大的Kestrel http服务器。

    框架引发异常,并由 AppHost.UncaughtExceptionHandlers

    System.InvalidOperationException: StatusCode cannot be set because the response has already started.
       at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame.ThrowResponseAlreadyStartedException(String value)
       at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame.set_StatusCode(Int32 value)
       at ServiceStack.HttpExtensions.EndRequestWithNoContent(IResponse httpRes)
       at ServiceStack.HttpResponseExtensionsInternal.WriteToResponse(IResponse httpRes, IRequest httpReq, Object result, Byte[] bodyPrefix, Byte[] bodySuffix, CancellationToken token)
       at ServiceStack.Host.RestHandler.<>c__DisplayClass13_1.<ProcessRequestAsync>b__1(Object response)
       at ServiceStack.AsyncExtensions.<>c__DisplayClass1_0`1.<ContinueClosure>b__0(Task innerTask)
    
    1. 此异常在我的方法返回后发生。它发生在框架代码中。
    2. 即使发生此错误,API仍能正确响应。

    更新 :升级到ServiceStack后。核心v5.0.2,它仍然存在。但是stacktrace不同。

    System.InvalidOperationException: StatusCode cannot be set because the response has already started.
       at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame.ThrowResponseAlreadyStartedException(String value)
       at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame.set_StatusCode(Int32 value)
       at ServiceStack.HttpExtensions.EndRequestWithNoContent(IResponse httpRes)
       at ServiceStack.HttpResponseExtensionsInternal.WriteToResponse(IResponse httpRes, IRequest httpReq, Object result, Byte[] bodyPrefix, Byte[] bodySuffix, CancellationToken token)
       at ServiceStack.Host.Handlers.ServiceStackHandlerBase.<HandleResponseNext>d__13.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at ServiceStack.Host.Handlers.ServiceStackHandlerBase.<HandleResponse>d__12.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at ServiceStack.Host.RestHandler.<ProcessRequestAsync>d__14.MoveNext()
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   mythz    6 年前

    此异常被阻止 by this commit 来自ServiceStack v5.0.3 now available on MyGet .