InvalidOperationException: The response headers cannot be modified because the response has already started

I have just recently published a fully netcore 2.2 app running InProcess on Windows Server. Using ServiceStack’s .Core package (5.4.0) and I have the following exceptions being thrown. Not sure what it’s about. Any ideas on how to stop it?

System.ArgumentException: Value does not fall within the expected range.
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.SetResponseHeaders()
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.ProduceStart(Boolean flushHeaders)
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.InitializeResponse(Boolean flushHeaders)
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.<>c__DisplayClass310_0.<<WriteAsync>g__WriteFirstAsync|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Server.IIS.Core.HttpResponseStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at Microsoft.AspNetCore.Server.IIS.Core.WrappingStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
   at ServiceStack.Text.JsonSerializer.SerializeToStream[T](T value, Stream stream)
   at ServiceStack.Serialization.JsonDataContractSerializer.SerializeToStream[T](T obj, Stream stream)
   at ServiceStack.Host.ContentTypes.<>c__DisplayClass30_0.<GetStreamSerializerAsync>b__0(IRequest httpReq, Object dto, Stream stream)
   at ServiceStack.HttpResponseExtensionsInternal.WriteToResponse(IResponse response, Object result, StreamSerializerDelegateAsync defaultAction, IRequest request, Byte[] bodyPrefix, Byte[] bodySuffix, CancellationToken token)
System.InvalidOperationException: The response headers cannot be modified because the response has already started.
   at Microsoft.AspNetCore.HttpSys.Internal.HeaderCollection.ThrowIfReadOnly()
   at Microsoft.AspNetCore.HttpSys.Internal.HeaderCollection.set_Item(String key, StringValues value)
   at Microsoft.AspNetCore.Http.Internal.DefaultHttpResponse.set_ContentType(String value)
   at ServiceStack.HttpResponseExtensionsInternal.WriteErrorToResponse(IResponse httpRes, IRequest httpReq, String contentType, String operationName, String errorMessage, Exception ex, Int32 statusCode)
   at ServiceStack.HostContext.RaiseAndHandleUncaughtException(IRequest httpReq, IResponse httpRes, String operationName, Exception ex)
   at ServiceStack.HttpResponseExtensionsInternal.HandleResponseWriteException(Exception originalEx, IRequest request, IResponse response, String defaultContentType)
   at ServiceStack.HttpResponseExtensionsInternal.WriteToResponse(IResponse response, Object result, StreamSerializerDelegateAsync defaultAction, IRequest request, Byte[] bodyPrefix, Byte[] bodySuffix, CancellationToken token)
   at ServiceStack.HttpResponseExtensionsInternal.WriteToResponse(IResponse response, Object result, StreamSerializerDelegateAsync defaultAction, IRequest request, Byte[] bodyPrefix, Byte[] bodySuffix, CancellationToken token)
   at ServiceStack.Host.Handlers.ServiceStackHandlerBase.HandleResponseNext(IRequest httpReq, IResponse httpRes, Object response)
   at ServiceStack.Host.Handlers.ServiceStackHandlerBase.HandleResponse(IRequest httpReq, IResponse httpRes, Object response)
   at ServiceStack.Host.Handlers.GenericHandler.ProcessRequestAsync(IRequest httpReq, IResponse httpRes, String operationName)

Headers are trying to be written whilst something has already started writing to the response. Which of your Services is causing it?

It is a standard service that is called when the user logs out.

There is nothing strange about it compared to any other service, but consistently when clients (angular client) calls this service these two exceptions happen and I do not know why.

What could be causing this as this is the first exception that seems to be thrown:

ArgumentException: Value does not fall within the expected range

You’d need the full Exception StackTrace to identify what’s throwing it.

Really not sure what could be causing this.

System.ArgumentException: Value does not fall within the expected range.
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.SetResponseHeaders()
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.ProduceStart(Boolean flushHeaders)
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.InitializeResponse(Boolean flushHeaders)
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.<>c__DisplayClass310_0.<<WriteAsync>g__WriteFirstAsync|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Server.IIS.Core.HttpResponseStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at Microsoft.AspNetCore.Server.IIS.Core.WrappingStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
   at ServiceStack.Text.JsonSerializer.SerializeToStream[T](T value, Stream stream)
   at ServiceStack.Serialization.JsonDataContractSerializer.SerializeToStream[T](T obj, Stream stream)
   at ServiceStack.Host.ContentTypes.<>c__DisplayClass30_0.<GetStreamSerializerAsync>b__0(IRequest httpReq, Object dto, Stream stream)
   at ServiceStack.HttpResponseExtensionsInternal.WriteToResponse(IResponse response, Object result, StreamSerializerDelegateAsync defaultAction, IRequest request, Byte[] bodyPrefix, Byte[] bodySuffix, CancellationToken token)

The Exception StackTrace indicates it occurs in an internal impl after flushing the JSON response to the Response Stream.

The StackTrace is of a normal JSON API request so would need a repro to find out what’s causing it.