Mr.Wang from Next Door
Mr.Wang from Next Door

Reputation: 14820

ServiceStack : InvalidOperationException: StatusCode cannot be set because the response has already started

Our API works fine, recently we changed from ServiceStack 4.5.14 to ServiceStack.Core 1.0.44 to take use of the powerful Kestrel http server.

An exception is thrown by the framework and caught by 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. This exception occurs after my method returns. It happens within the framework code.
  2. Even when this error occurs, the API still response correctly.

UPDATE: After upgrading to ServiceStack.Core v5.0.2, it still occurs. However stacktrace is different.

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()

Upvotes: 2

Views: 1402

Answers (1)

mythz
mythz

Reputation: 143399

This Exception is prevented by this commit from ServiceStack v5.0.3 that's now available on MyGet.

Upvotes: 1

Related Questions