aspnet / AspNetKatana

Microsoft's OWIN implementation, the Katana project
Apache License 2.0
967 stars 333 forks source link

InvalidOperationException at set_ContentLength64 inside HttpMessageHandlerAdapter #457

Closed abatishchev closed 2 years ago

abatishchev commented 2 years ago

We're observing numerous exceptions like this (the bottom the stack trace may vary per different middleware) while we never attempt to modify ContentLength:

System.InvalidOperationException: This operation cannot be performed after the response has been submitted.
   at System.Net.HttpListenerResponse.set_ContentLength64(Int64 value)
   at Microsoft.Owin.Host.HttpListener.RequestProcessing.HeadersDictionaryBase.Set(String key, String[] value)
   at System.Web.Http.Owin.HttpMessageHandlerAdapter.SendResponseMessageAsync(HttpRequestMessage request, HttpResponseMessage response, IOwinResponse owinResponse, CancellationToken cancellationToken)
   at System.Web.Http.Owin.HttpMessageHandlerAdapter.<InvokeCore>d__20.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 Microsoft.Owin.Security.Infrastructure.AuthenticationMiddleware`1.<Invoke>d__5.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 Microsoft.AAD.Throttling.GatewayService.Middleware.Reporting.LoggerMiddleware.<Invoke>d__8.MoveNext() in C:\source\src\Service\GatewayService\Middleware\Reporting\LoggerMiddleware.cs:line 44

Environment:

.NET Framework 4.6.2
Azure Service Fabric

Packages:

<PackageReference Include="Microsoft.Owin" Version="4.2.0" />
<PackageReference Include="Microsoft.Owin.FileSystems" Version="4.2.0" />
<PackageReference Include="Microsoft.Owin.Host.HttpListener" Version="4.2.0" />
<PackageReference Include="Microsoft.Owin.Hosting" Version="4.2.0" />
<PackageReference Include="Microsoft.Owin.Security" Version="4.2.0" />
<PackageReference Include="Microsoft.Owin.Security.ActiveDirectory" Version="4.2.0" />
<PackageReference Include="Microsoft.Owin.StaticFiles" Version="4.2.0" />
abatishchev commented 2 years ago

Looks like the error is gone after updating to 4.2.1.