ServiceStack:InvalidOperationException:无法设置StatusCode,因为响应已经开始

时间:2017-12-27 07:32:18

标签: servicestack

我们的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仍会正确响应。
  3. UPDATE :升级到ServiceStack.Core v5.0.2后,仍然会出现。然而,堆栈跟踪是不同的。

    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 个答案:

答案 0 :(得分:0)

by this commit的ServiceStack v5.0.3中阻止now available on MyGet此异常。