question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ASPNETCORE Serilog : Failed invoking event handler: System.ObjectDisposedException: IFeatureCollection has been disposed.

See original GitHub issue

Please mark the type framework used:

  • ASP.NET MVC
  • ASP.NET Web API (OWIN)
  • ASP.NET Core
  • WPF
  • WinForms
  • Xamarin
  • Other:

Please mark the type of the runtime used:

  • .NET Framework
  • Mono
  • .NET Core
  • Version: netcoreapp3.1

Please mark the NuGet packages used:

  • Sentry
  • Sentry.Serilog
  • Sentry.NLog
  • Sentry.Log4Net
  • Sentry.Extensions.Logging
  • Sentry.AspNetCore
  • Version: 2.1.8

I’m running a small process with Sentry / Serilog and it seems to work fine other that the fact that I get this error in my message breadcrumbs:

Failed invoking event handler: System.ObjectDisposedException: IFeatureCollection has been disposed. 
Object name: 'Collection'.
    at Microsoft.AspNetCore.Http.Features.FeatureReferences`1.ThrowContextDisposed()
    at Microsoft.AspNetCore.Http.Features.FeatureReferences`1.ContextDisposed()
    at Microsoft.AspNetCore.Http.Features.FeatureReferences`1.Fetch[TFeature](TFeature& cached, Func`2 factory)
    at Microsoft.AspNetCore.Http.DefaultHttpContext.get_TraceIdentifier()
    at Sentry.AspNetCore.ScopeExtensions.Populate(Scope scope, HttpContext context, SentryAspNetCoreOptions options)
    at Sentry.AspNetCore.SentryMiddleware.PopulateScope(HttpContext context, Scope scope)
    at Sentry.AspNetCore.SentryMiddleware.<>c__DisplayClass8_1.<InvokeAsync>b__3(Object _, EventArgs __)
    at Sentry.Scope.Evaluate()

Below is the code I use to build the WebHost:

return WebHost.CreateDefaultBuilder()
                .UseShutdownTimeout(TimeSpan.FromSeconds(SHUTDOWN_TIMEOUT_IN_SECONDS))
                .UseConfiguration(configuration)
                .UseUrls($"http://*:{parameters.PrivatePort}")
                .PreferHostingUrls(true) // Make sure the WebHostBuilder uses the URLs set in UseUrls() instead of the Kestrel ones
                .ConfigureServices((hostBuilder, services) =>
                {
                    services.Configure<HostOptions>(o => o.ShutdownTimeout = TimeSpan.FromSeconds(SHUTDOWN_TIMEOUT_IN_SECONDS));
                    services.AddSingleton(childProcess);
                    services.AddSingleton(metricsHandler);
                })
                .UseStartup<ChildStartup>()
                .UseSerilog()
                .UseSentry(o =>
                {
                    o.Environment = parameters.Environment;
                })

And the relevant configuration is here:

  "Sentry": {
    "Dsn": "MYDSN",
    "AttachStackTrace": true
  },
  "Serilog": {
    "Properties": {
      "source_agent": "SomonGameChild"
    },
    "WriteTo": [
      {
        "Name": "Sentry",
        "Args": {
          "MinimumBreadcrumbLevel": "Debug",
          "MinimumEventLevel": "Warning"
        }
      }
    ]
  }

Any help is appreciated. Let me know if you need more information.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
madzafcommented, Jan 29, 2021

Just as a final comment I got sourcelink to work and I think what happens is what I kind of suspected, my background service logs a warning, and the sdk tries to recover a spent http request when that warning occurs but the request is already over. I’ve added the breadcrumb removal and it works perfectly. Thanks for all the help.

1reaction
madzafcommented, Jan 28, 2021

Ah I didn’t know about sourcelink ! I’ll look into it and try to see exactly when this happens. Thank you for the breadcrumb removal tip, it should be a good workaround in the mean time. I’ll post more here if I get more information by debbugging

Read more comments on GitHub >

github_iconTop Results From Across the Web

IFeatureCollection has been disposed. in Asp.net Core ...
Scoped services are disposed at the request's end. The main problem is OnChanged can be called after the request's end.
Read more >
Synchronous operations are disallowed. Call ReadAsync ...
ASPNETCORE Serilog : Failed invoking event handler: System.ObjectDisposedException: IFeatureCollection has been disposed.
Read more >
IFeatures Collection has been disposed, object Name
Hi Dear All, i have created a web api project and created a new web api named login, now i created asp.net core...
Read more >
ObjectDisposedException Class (System)
The exception that is thrown when an operation is performed on a disposed object. public ref class ObjectDisposedException : InvalidOperationException. C# Copy.
Read more >
Context keyword doesn't get recognized in ASP.net 5 MVC ...
Coding example for the question Context keyword doesn't get recognized in ASP.net 5 MVC Controller for use with Sessions.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found