aspnetcore: Intermittent ObjectDisposedException in SPA Hosting

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I have a small aspnetcore service to host/serve a SPA. Only intermittently, I am getting the following error in the event log.

Category: Microsoft.AspNetCore.Server.HttpSys.HttpSysListener
EventId: 6
SpanId: 9e2d8fa91d335924
TraceId: 9590e80341d42cca7b750ac04d095314
ParentId: 0000000000000000
RequestId: 4000002b-0005-ed00-b63f-84710c7967bb
RequestPath: /app/path/to/a/static/file.ext

ProcessRequestAsync

Exception: 
System.ObjectDisposedException: ObjectDisposed_Generic
ObjectDisposed_ObjectName_Name, Microsoft.AspNetCore.Server.HttpSys.ResponseBody
   at Microsoft.AspNetCore.Server.HttpSys.ResponseBody.CheckDisposed()
   at Microsoft.AspNetCore.Server.HttpSys.ResponseBody.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
   at System.IO.Stream.WriteAsync(ReadOnlyMemory`1 buffer, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Server.HttpSys.ResponseStream.WriteAsync(ReadOnlyMemory`1 buffer, CancellationToken cancellationToken)
   at System.IO.Pipelines.StreamPipeWriter.FlushAsyncInternal(Boolean writeToStream, ReadOnlyMemory`1 data, CancellationToken cancellationToken)
   at System.IO.Pipelines.StreamPipeWriter.CompleteAsync(Exception exception)
   at Microsoft.AspNetCore.Server.HttpSys.RequestContext.CompleteAsync()
   at Microsoft.AspNetCore.Server.HttpSys.RequestContext`1.ExecuteAsync()
   at Microsoft.AspNetCore.Server.HttpSys.RequestContext`1.ExecuteAsync()

However, the app runs in the browser without any issue. This problem is more prominent when running the automated e2e tests (using Playwright).

This is how I am configuring the SPA static files.

public class Startup
{
  public void ConfigureServices(IServiceCollection services)
  {
    services.AddSpaStaticFiles(opt =>
    {
      opt.RootPath = _Configuration.GetValue("Webroot", "./Content");
    });
  }

  public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger, IConfiguration config)
  {
    app.UseSpaStaticFiles(new StaticFileOptions
    {
      OnPrepareResponse = context =>
      {
        if (context.Context.Response.HasStarted || context.Context.Response.StatusCode != (int)HttpStatusCode.OK) return;
        context.Context.Response.Headers["Cache-Control"] = "public, max-age=1000";
        context.Context.Response.Headers["Vary"] = "Accept-Encoding";
      }
    });
  }
}

Expected Behavior

The superfluous (as there is no error at the client/browser) error log should be avoided.

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

6.0.301

Anything else?

No response

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 1
  • Comments: 15 (9 by maintainers)

Most upvoted comments

@adityamandaleeka @Tratcher @mkArtakMSFT @javiercn please consider reopening this.

@Sayan751 thanks for contacting us.

We have changed the way we approach SPAs on 6.0, we recommend publishing the contents to the wwwroot folder and using app.MapFallbackToFile to serve the SPA as a fallback. You can check a freshly created template for reference on how to update it.

Let us know if you experience issues after making the suggested changes.