-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Closed
Description
Is there an existing issue for this?
- I have searched the existing issues
Description
Calling a simple repository query in a BackgroundJob causes a System.ObjectDisposedException.
System.ObjectDisposedException: Cannot access a disposed context instance. A common cause of this error is disposing a context instance 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 instance, or wrapping it 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: 'BookStoreDbContext'.
at Microsoft.EntityFrameworkCore.DbContext.CheckDisposed()
at Microsoft.EntityFrameworkCore.DbContext.get_ContextServices()
at Microsoft.EntityFrameworkCore.DbContext.get_Model()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.CheckState()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Collections.Generic.IEnumerable<TEntity>.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Acme.BookStore.Books.BooksBackgroundJob.ExecuteAsync(BooksJobArgs args) in C:\Code\Acme.BookStore\Acme.BookStore\src\Acme.BookStore.Application\Books\BooksBackgroundJob.cs:line 24
This is the code that causes the exception:
var books = (await _repository.GetQueryableAsync())
.Where(b => b.Name.StartsWith(args.Name))
.ToList();Reproduction Steps
- Create a new project called Acme.BookStore.
- Add a
BooksBackgroundJob.csfile with this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
namespace Acme.BookStore.Books;
public class BooksBackgroundJob : AsyncBackgroundJob<BooksJobArgs>, ITransientDependency
{
private readonly IRepository<Book> _repository;
public BooksBackgroundJob(IRepository<Book> repository)
{
_repository = repository;
}
public override async Task ExecuteAsync(BooksJobArgs args)
{
try
{
var books = (await _repository.GetQueryableAsync())
//.Where(b => b.Name.StartsWith(args.Name))
.ToList();
System.Diagnostics.Trace.WriteLine("Found " + books.Count);
}
catch (Exception ex) {
System.Diagnostics.Trace.WriteLine(ex);
}
System.Diagnostics.Trace.WriteLine("Finished.");
}
}
- Add a
BooksJobArgs.csfile with this:
namespace Acme.BookStore.Books;
public class BooksJobArgs
{
public string Name { get; set; }
}
- Start a new job by calling this `_backgroundJobManager.EnqueueAsync(new BooksJobArgs() { Name = "test" });
`
Expected behavior
It should not throw this exception.
Actual behavior
No response
Regression?
No response
Known Workarounds
No response
Version
9.1.0
User Interface
Common (Default)
Database Provider
EF Core (Default)
Tiered or separate authentication server
None (Default)
Operation System
Windows (Default)
Other information
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels