Cannot access a disposed object - DI Service Provider

See original GitHub issue

Hi all!

I’ve just started moving an application from ASP.NET Core 2.0, to Core 2.1. Everything went smooth and is working fine, except a quite important function that I had to disable for now to make the app run.

I use SignalR in this project, and have implemented a HeartBeat method, to keep the connection alive (running the app behind IIS). The solution I have implemented is following this thread on StackOverflow: https://stackoverflow.com/questions/47208844/net-core-signalr-connection-timeout-heartbeat-timer-connection-state-chan

This has been working great when running the app on .NET Core 2.0.x, but now throws the following error:

System.ObjectDisposedException
  HResult=0x80131622
  Message=Cannot access a disposed object.
  Source=Microsoft.Extensions.DependencyInjection
  StackTrace:
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ThrowHelper.ThrowObjectDisposedException()
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
   at WebApplication.Startup.<>c__DisplayClass5_0.<<Configure>b__3>d.MoveNext() in C:\Users\MyPath\DotNetCoreTest2\Startup.cs:line 160

The part of the code throwing this error is as described above, the HeartBeat method, that looks like this

TimerCallback SignalRHeartBeat = async (x) => {   
    await serviceProvider.GetService<IHubContext<TheHubClass>>().Clients.All.InvokeAsync("Heartbeat", DateTime.Now); };
    var timer = new Timer(SignalRHeartBeat).Change(TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(30));

And is placed in Startup.cs under the Configure() method, The serviceProvider is injected with DI using IServiceProvider serviceProvider like this public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)

Has something changed in .NET Core 2.1 causing this?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:19 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
jjxtracommented, Sep 4, 2019

If you have a transient service constructor that takes an IServiceProvider, the service provider will be disposed. This is a bug in .NET core.

Work-around is to manage your own transient life-cycle in a pooling class.

2reactions
mmikirtumovcommented, Jun 9, 2018

Hi @davidfowl ,

We have a same problem. In our .net core 2.0 background service works fine. When we updated our .net core version to 2.1 system brought us the following error

Cannot access a disposed object. Object name: 'IServiceProvider'.

We have the following code _serviceProvider.CreateScope(). Could you please tell me when are you going to rollback your changes https://github.com/aspnet/Hosting/pull/1106 and in wich version it will be available?

UPD: using app.ApplicationServices solved the problem, but I do not think that this is good workaround. Still waiting the undo of changes https://github.com/aspnet/Hosting/pull/1106. Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot access a disposed object. A common cause of this ...
A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the...
Read more >
Resolving disposed ServiceProvider throws exception - .NET
NET 6, an ObjectDisposedException is thrown when a service is resolved after the service provider has been disposed, and there's no deadlock.
Read more >
Cannot access a disposed object in ASP.NET Core when ...
A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the...
Read more >
DBContext- Cannot access a disposed object in ASP.NET ...
System.ObjectDisposedException: Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection ...
Read more >
Cannot access a disposed context instance #1753 | Support ...
Hello I am getting this error when I try to use a EF Core Repository which is resolved using ServiceProvider in CustomTenantResolver.
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