DBContext Issue with 3.3.3: System.ObjectDisposedException: Cannot access a disposed object.
See original GitHub issueDescribe the bug
Each time my job fires, I get the error:
---> System.ObjectDisposedException: 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 if 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: 'MyDbContext'.
Version used
3.3.3
To Reproduce
Here is my setup in my Startup.cs
:
services.AddQuartz(q =>
{
// Allows us to use dependency injection in all our job classes
q.UseMicrosoftDependencyInjectionJobFactory();
// Run a job every minute
var myJobKey = new JobKey("MyJob");
q.AddJob<MyJob>(opts => opts
.WithIdentity(myJobKey));
q.AddTrigger(opts => opts
.ForJob(myJobKey)
.WithIdentity("MyJobTrigger")
.WithCronSchedule("0 0/1 * * * ?"));
});
services.AddQuartzServer(options =>
{
// when shutting down we want jobs to complete gracefully
options.WaitForJobsToComplete = true;
});
Expected behavior
According to the docs, using:
q.UseMicrosoftDependencyInjectionJobFactory();
should:
also injects scoped services (like EF DbContext) without problems
Additional context
I should also mention I am using aspnetboilerplate, perhaps that is the culprit? Though I’m configuring everything with Quartz through Startup.cs
, so actually aspnetboilerplate shouldn’t come into play at all here…
Not sure what I’m missing. Any pointers, ideas, or help would be much appreciated!
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Cannot access a disposed object. A common cause of this ...
System.ObjectDisposedException: Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from ...
Read more >Cannot access a disposed context instance in var result = ...
This type of error can happen if the DbContext has been disposed. Most likely the error is elsewhere in your code. Also, please...
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 >[Solved] Cannot access a disposed object. A common ...
Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then...
Read more >Cannot access a disposed object in ASP.NET Core when ...
System.ObjectDisposedException: Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Finally, to be clear, I’ve gone with my original solution, using Quartz in
Startup.cs
. I just needed to add [UnitofWork] to each job using repositories, as well as ensuring my job extendedITransientDependency
, i.e.MyJob : IJob, ITransientDependency
(IJob
so Quartz is happy andITransientDependency
so ASP.NET boilerplate is happy) Everything seems to be running well (so far).Ahhh, this may also be relevant to whoever finds this: https://github.com/aspnetboilerplate/aspnetboilerplate/issues/3757
Need to add
[UnitOfWork]
to the class.