Skip to content

Cannot access a disposed object - DI Service Provider #3076

Closed
@roos-robert

Description

@roos-robert

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions