question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DBContext Issue with 3.3.3: System.ObjectDisposedException: Cannot access a disposed object.

See original GitHub issue

Describe 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

docs link: https://www.quartz-scheduler.net/documentation/quartz-3.x/packages/microsoft-di-integration.html#di-aware-job-factories

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:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
princefishthrowercommented, Dec 9, 2021

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 extended ITransientDependency, i.e. MyJob : IJob, ITransientDependency (IJob so Quartz is happy and ITransientDependency so ASP.NET boilerplate is happy) Everything seems to be running well (so far).

1reaction
princefishthrowercommented, Dec 9, 2021

Ahhh, this may also be relevant to whoever finds this: https://github.com/aspnetboilerplate/aspnetboilerplate/issues/3757

Need to add [UnitOfWork] to the class.

Read more comments on GitHub >

github_iconTop 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 >

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