CacheCow: Writing to the response body is invalid for responses with status code 304.

When I test with Postman sending the If-None-Match value, I get the 304 response code, but my Asp Core app logs the following error: Writing to the response body is invalid for responses with status code 304.

Here is the stack trace: [2019-11-26 15:27:43 Error] - 0HLRIMTOD75D9:00000002 -> Microsoft.AspNetCore.Server.Kestrel : Connection id "“0HLRIMTOD75D9"”, Request id "“0HLRIMTOD75D9:00000002"”: An unhandled exception was thrown by the application. System.InvalidOperationException: Writing to the response body is invalid for responses with status code 304. at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ThrowWritingToResponseBodyNotSupported() at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.WriteAsync(ReadOnlyMemory1 data, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) at System.IO.MemoryStream.CopyToAsync(Stream destination, Int32 bufferSize, CancellationToken cancellationToken) at CacheCow.Server.CachingPipeline.After(HttpContext context, Object viewModel) at CacheCow.Server.Core.Mvc.HttpCacheFilter.OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at AspNetCoreRateLimit.RateLimitMiddleware1.Invoke(HttpContext context) at Microsoft.AspNetCore.Server.IISIntegration.IISMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Builder.Extensions.UsePathBaseMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 29 (16 by maintainers)

Commits related to this issue

Most upvoted comments

I think this angry tweet sums up the situation… https://twitter.com/aliostad/status/1232075169008627714?s=20

Checking for 304 Response prevents the deeper exception. Haven;t dug in to see what is causing ms.Length > 0 though.

finally { if (mustReflush && context.Response.StatusCode != StatusCodes.Status304NotModified) { ms.Position = 0; await ms.CopyToAsync(context.Response.Body); } }

Same. worked for me when I upgraded to latest packages of cachecow. Thanks!!

Found It! I had not updated my web.config when migrating from aspnetcore 2.1 to 2.2, There is a change that needs to made to the handler aspNetCore: the modules property needs to reference “AspNetCoreModuleV2”.

Here is a link explaining the changes: https://discuss.newrelic.com/t/relic-solution-changes-with-net-core-2-2-and-aspnetcoremodulev2/65232

Thanks for looking into.

I have a feeling this has to do with the optimisations that the ASP.NET team have done in terms of sending the headers early. This creates a variety of problems in CacheCow and I have discussed this at length with David Fowler and he promised to add an option to turn it off but it has not been done.

It might also be something different but due to its inconsistent occurrence, I think it is that.

Anyway, I will keep the issue open and try to build a testbed for it to try under varying load.