Multiple calls to the same endpoint results in a 'headers have already been sent' exception, how do I get around it

-1

This is kind of a 3 part question, which I'll summarise now before going into detail (question 3 is the most important and a resolution to that will be sufficient for acceptance):

  1. Why is an endpoint getting called multiple times when the client only makes a call once?
  2. Why does that result in a System.Web.HttpException (0x80004005): headers have already been sent?
  3. What are my options for getting around this issue?

Further detail:

I'm working on an Asp.Net Framework WebAPI, which when debugging suggests that certain GET endpoints are getting called more than once in quick succession. I can't seem to tell if these are specific endpoints or whether it's completely random which ones get called multiple times. Debugging has produced indeterminate results so far.

Regardless, when an endpoint does get called more than once, it always results in the following exception:

System.Web.HttpException (0x80004005): headers have already been sent
    at System.Web.HttpResponse.set_StatusCode (System.Int32 value) [0x00008] in /Users/builder/jenkins/workspace/build-package-osx-mono/2019-06/external/bockbuild/builds/mono-x64/mcs/class/System.Web/System.Web/HttpResponse.cs:414 
    at System.Web.HttpResponseWrapper.set_StatusCode (System.Int32 value) [0x00000] in /Users/builder/jenkins/workspace/build-package-osx-mono/2019-06/external/bockbuild/builds/mono-x64/mcs/class/System.Web/System.Web/HttpResponseWrapper.cs:155 
    at Microsoft.Owin.Host.SystemWeb.OwinCallContext.Microsoft.Owin.Host.SystemWeb.CallEnvironment.AspNetDictionary.IPropertySource.SetResponseStatusCode (System.Int32 value) [0x00000] in <392be445bb6947f2a5f768f7860b8204>:0 
    at Microsoft.Owin.Host.SystemWeb.CallEnvironment.AspNetDictionary.set_ResponseStatusCode (System.Int32 value) [0x00000] in <392be445bb6947f2a5f768f7860b8204>:0 
    at Microsoft.Owin.Host.SystemWeb.CallEnvironment.AspNetDictionary.PropertiesTrySetValue (System.String key, System.Object value) [0x00257] in <392be445bb6947f2a5f768f7860b8204>:0 
    at Microsoft.Owin.Host.SystemWeb.CallEnvironment.AspNetDictionary.System.Collections.Generic.IDictionary<System.String,System.Object>.set_Item (System.String key, System.Object value) [0x00000] in <392be445bb6947f2a5f768f7860b8204>:0 
    at Microsoft.Owin.OwinResponse.Set[T] (System.String key, T value) [0x00006] in <5decccfc95e44573a9e45d83dbe60e91>:0 
    at Microsoft.Owin.OwinResponse.set_StatusCode (System.Int32 value) [0x00000] in <5decccfc95e44573a9e45d83dbe60e91>:0 
    at System.Web.Http.Owin.HttpMessageHandlerAdapter.SendResponseMessageAsync (System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpResponseMessage response, Microsoft.Owin.IOwinResponse owinResponse, System.Threading.CancellationToken cancellationToken) [0x00007] in <3af1be95d6624fee83df21798930c383>:0 
    at System.Web.Http.Owin.HttpMessageHandlerAdapter.InvokeCore (Microsoft.Owin.IOwinContext context, Microsoft.Owin.IOwinRequest owinRequest, Microsoft.Owin.IOwinResponse owinResponse) [0x00431] in <3af1be95d6624fee83df21798930c383>:0 
    at Owin.OwinExtensions+<>c__DisplayClass2.<UseAutofacMiddleware>b__0 (Microsoft.Owin.IOwinContext context, System.Func`1[TResult] next) [0x00103] in <098fe0c316b949598cd8e54465ad5c46>:0 
    at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContextStage.RunApp (System.Func`2[T,TResult] entryPoint, System.Collections.Generic.IDictionary`2[TKey,TValue] environment, System.Threading.Tasks.TaskCompletionSource`1[TResult] tcs, Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult result) [0x0007e] in <392be445bb6947f2a5f768f7860b8204>:0 
    at Microsoft.Owin.Security.Infrastructure.AuthenticationMiddleware`1[TOptions].Invoke (Microsoft.Owin.IOwinContext context) [0x00183] in <f9be925b4bc648358fe1d2df64fcfd52>:0 
    at Microsoft.Owin.Security.Infrastructure.AuthenticationMiddleware`1[TOptions].Invoke (Microsoft.Owin.IOwinContext context) [0x00183] in <f9be925b4bc648358fe1d2df64fcfd52>:0 
    at Microsoft.Owin.Security.Infrastructure.AuthenticationMiddleware`1[TOptions].Invoke (Microsoft.Owin.IOwinContext context) [0x00183] in <f9be925b4bc648358fe1d2df64fcfd52>:0 
    at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContextStage.RunApp (System.Func`2[T,TResult] entryPoint, System.Collections.Generic.IDictionary`2[TKey,TValue] environment, System.Threading.Tasks.TaskCompletionSource`1[TResult] tcs, Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult result) [0x0007e] in <392be445bb6947f2a5f768f7860b8204>:0 
    at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.DoFinalWork (Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult result) [0x0007d] in <392be445bb6947f2a5f768f7860b8204>:0 
    at Microsoft.Owin.Host.SystemWeb.Infrastructure.ErrorState.Rethrow () [0x00000] in <392be445bb6947f2a5f768f7860b8204>:0 
    at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End (System.IAsyncResult ar) [0x0001d] in <392be445bb6947f2a5f768f7860b8204>:0 
    at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork (System.IAsyncResult ar) [0x00006] in <392be445bb6947f2a5f768f7860b8204>:0 
    at System.Web.AsyncInvoker.<doAsyncCallback>b__8_0 (System.Object ores) [0x00007] in /Users/builder/jenkins/workspace/build-package-osx-mono/2019-06/external/bockbuild/builds/mono-x64/mcs/class/System.Web/System.Web/HttpApplication.cs:1963


As far as I"m aware, this does not happen when the API executes within a Windows environment, so that suggests to me it could be a Mono issue. This issue could be related: C# Google OAUTH2 using OWIN on Mono results in "System.Web.HttpException (0x80004005): headers have already been sent"


Versions:

  • Asp.Net Framework 4.7.2
  • Mono runtime 6.4.0.208 (2019-06/07c23f2ca43) (64-bit)

Not sure what other version info or configuration is relevant, but happy to provide more details upon request.

asp.net-web-api
mono
owin
asked on Stack Overflow Nov 18, 2019 by Ash

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0