Hi i just setup Hangfire in a aspnet core project with AutoFac.
But i got this problem when running a RecurringJob.
“Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur is you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.”
My setup: var builder = new ContainerBuilder(); builder.RegisterType<BookingCalendarDbContext>().InstancePerLifetimeScope(); builder.Populate(services); var container = builder.Build(); GlobalConfiguration.Configuration.UseAutofacActivator(container, false)
Message: Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur is you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
Object name: ‘BookingCalendarDbContext’.
StackTrace:
at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
at Microsoft.EntityFrameworkCore.DbContext.get_StateManager()
at Microsoft.EntityFrameworkCore.DbContext.EntryWithoutDetectChanges[TEntity](TEntity entity)
at Microsoft.EntityFrameworkCore.DbContext.SetEntityState[TEntity](TEntity entity, EntityState entityState)
at BookingCalendar.Data.CalendarCore.Repositories.CalendarEventRepository.Add(CalendarEvent calendarEvent) in C:\Code\BookingCalendar\src\BookingCalendar.Data\CalendarCore\Repositories\CalendarEventRepository.cs:line 59
at BookingCalendar.Core.CalendarCore.Services.Implementation.CalendarEventService.Create(CalendarEvent calendarEvent) in C:\Code\BookingCalendar\src\BookingCalendar.Core\CalendarCore\Services\Implementation\CalendarEventService.cs:line 21
at BookingCalendar.Core.CalendarCore.Services.Implementation.CalendarICalService.AddOrUpdateICalEvents(CalendarICal iCal, IReadOnlyList`1 existingCalendarEventsToProcess, IReadOnlyList`1 existingCalendarEventsWithICalToProcess, IReadOnlyList`1 newICalEvents) in C:\Code\BookingCalendar\src\BookingCalendar.Core\CalendarCore\Services\Implementation\CalendarICalService.cs:line 159
at BookingCalendar.Core.CalendarCore.Services.Implementation.CalendarICalService.ImportEvents(CalendarICal iCal, IReadOnlyList`1 calendarEvents, IReadOnlyList`1 newICalEvents) in C:\Code\BookingCalendar\src\BookingCalendar.Core\CalendarCore\Services\Implementation\CalendarICalService.cs:line 115
at BookingCalendar.Core.CalendarCore.Services.Implementation.CalendarICalService.<>c__DisplayClass3_0.<<Import>b__0>d.MoveNext() in C:\Code\BookingCalendar\src\BookingCalendar.Core\CalendarCore\Services\Implementation\CalendarICalService.cs:line 32
No there is no exception in jobs dashboard, because the application fails and closes.
public class BookingCalendarDbContext : IdentityDbContext<User> { public BookingCalendarDbContext(DbContextOptions<BookingCalendarDbContext> options) : base(options) { }
@fatihk, really sorry, I wanted to ask about source code of the recurring job that is failing to know how do you pass the dependencies, but copy-pasted the BookingCalendarDbContext
When i run the job from the hangfire dashboard it is hitting the right service and so on, the problem is when i call a certain repository and try to save something to the database then it is saying that the DbContext is disposed. I assume it is something with the setup?
The BookingCalendarDbContext service registration is ok (InstancePerLifetimeScope). Hangfire.Autofac will create a new scope before executing just another background job, and will dispose the scope (and dispose the db context) just after the processing.
How the implementation of the ICalendarICalService interface is registered? If you are using singleton registration, then you’ll end up with exactly your problem, because there will be only 1 shared object with a disposed database context.
I called the BookingCalendarDbContext inside a constructor with .Dispose() like this. public CalendarEventRepository(BookingCalendarDbContext db) { _db = db; _db.Dispose(); }
@odinserj I found the problem, it was because i was doing some async work in my service and i haven’t applied async to the method that was called from the RecurringJob it is now working.