HangfireIO / HangfireIO/Hangfire

Cannot access a disposed object (LoggerFactory)

Open
#2,147 10 comments 2 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
10.1k
Forks
1.8k
Avg merge
1h 19m
Merged PRs (30d)
1

Description

We are using .NET 6, [Hangfire.AspNetCore 1.7.31](https://www.nuget.org/packages/Hangfire.AspNetCore/1.7.31) and [Hangfire.InMemory 0.3.4](https://github.com/HangfireIO/Hangfire.InMemory/). This is how we configure Hangfire in `Startup.cs`:

```cs
public void ConfigureServices(IServiceCollection services)
{
// ...

services.AddHangfire(configuration =>
{
configuration.UseInMemoryStorage();
});

services.AddHangfireServer(x =>
{
x.WorkerCount = 5;
});

// ...
}
```

We have a unit test which calls `RecurringJob.RemoveIfExists("jobId")` somewhere in its body. This test was working for a long time and failed randomly today. We re-ran the test multiple times but we were unable to reproduce the exception.

Error message:
```
System.ObjectDisposedException : Cannot access a disposed object.
Object name: 'LoggerFactory'.
```

Stacktrace:
```
at Microsoft.Extensions.Logging.LoggerFactory.CreateLogger(String categoryName)
at Hangfire.AspNetCore.AspNetCoreLogProvider.GetLogger(String name)
at Hangfire.Logging.LogProvider.GetLogger(String name)
at Hangfire.Logging.LogProvider.GetLogger(Type type)
at Hangfire.Client.CoreBackgroundJobFactory..ctor(IStateMachine stateMachine)
at Hangfire.Client.BackgroundJobFactory..ctor(IJobFilterProvider filterProvider)
at Hangfire.RecurringJobManager..ctor(JobStorage storage, IJobFilterProvider filterProvider, ITimeZoneResolver timeZoneResolver, Func`1 nowFactory)
at Hangfire.RecurringJobManager..ctor(JobStorage storage, IJobFilterProvider filterProvider, ITimeZoneResolver timeZoneResolver)
at Hangfire.RecurringJobManager..ctor(JobStorage storage, IJobFilterProvider filterProvider)
at Hangfire.RecurringJobManager..ctor(JobStorage storage)
at Hangfire.RecurringJobManager..ctor()
at Hangfire.RecurringJob.<>c.<.cctor>b__21_0()
at System.Lazy`1.PublicationOnlyViaFactory(LazyHelper initializer)
at System.Lazy`1.CreateValue()
at Hangfire.RecurringJob.RemoveIfExists(String recurringJobId)
at OurApplication.Service.BL.Identity.OurServiceBL.DeleteHangfireRecurringJobIfHangfireIsEnabled(String jobId)
```

Why did the exception occur? How can we prevent it from happening again?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.