Correctly using of dependency injected DbContext to query Database

0

I have set up my own context using (as I believe is correct):

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContextPool<JobTrackingContext>(options => options.UseNpgsql(connection));
    services.AddScoped<IJobRepository, JobRepository>();
}

Then I define my JobTrackingContext as follows:

public JobTrackingContext(DbContextOptions<JobTrackingContext> options)
            : base(options)
{
    public DbSet<Job> Jobs { get; set; }
}

Now I can define a repository to actually create/edit/delete Jobs:

 public class JobRepository : GenericRepository<Job, long>, IJobRepository
{
        private Job currentJob;
        public JobRepository(JobTrackingContext jobTrackingContext, JobTrackingSettings settings)
        : base(jobTrackingContext)
    {
        _settings = settings;
    }
        public async Task StartSync(JobType jobType, JobTriggerType jobTriggerType)
        {
            var tempJob = new Job(jobType, jobTriggerType);
            await _dbContext.Jobs.AddAsync(tempJob);
            await _dbContext.SaveChangesAsync();
        }
}

And all this code gets instantiated by a Post-request to this API:

public async void Post()
{
    _logger.LogDebug("Going to start account sync");
    await _jobRepository.StartSync(JobType.ZRequestSync, JobTriggerType.Scheduled);
    try
    {
        await _sync.StartAsync();
        await _jobRepository.ChangeSyncStatus(JobStatusType.Finished);
    }
    catch (Exception e)
    {
        _logger.LogError(e, "Error occured during sync :(");
        await _jobRepository.ChangeSyncStatus(JobStatusType.Failed);
    }
}

Yet when I do this, I get an Exception with the message Reset() called on connector with state Connecting. I do not understand where this comes from. When I do not use the injected version, but instead do this:

using (var c = new JobTrackingContext())
{
    var job = new Job(jobType, jobTriggerType)
    await c.Jobs.AddAsync(job);
    await c.SaveChangesAsync();
}

All seems to be working fine. It seems that the context gets disposed too early. But how can I prevent this and/or what am I missing?

The Full Stacktrace:

    System.ObjectDisposedException
  HResult=0x80131622
  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 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.
  Source=Microsoft.EntityFrameworkCore
  StackTrace:
   at Microsoft.EntityFrameworkCore.DbContext.CheckDisposed()
   at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
   at Microsoft.EntityFrameworkCore.DbContext.<SaveChangesAsync>d__52.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()
   at ZShared.JobRepository.<StartSync>d__4.MoveNext() in C:\Users\richa\Documents\Codes\Company\Product\Shared\Folder\JobRepository.cs:line 38
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at ZAccountSyncService.AccountSyncController.<Post>d__4.MoveNext() in C:\Users\richa\Documents\Code\Company\Product\SubProduct\AccountSyncController.cs:line 32
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
asp.net
entity-framework
dependency-injection
.net-core
asked on Stack Overflow Mar 11, 2019 by Rich_Rich • edited Mar 11, 2019 by Rich_Rich

1 Answer

2

The gist of the problem is the declaration of your AccountSyncController.Post method. You have async void Post() instead of async Task Post().

When there is no task, a request has nothing to await and ends before the call of method _sync.StartAsync() is completed. With an end of a request comes also the end of a lifetime scope. On lifetime scoped end, all instances with scoped lifetime get disposed. Thus, your context is disposed before you get to the call of SaveChanges method. And this is the cause of your exception.

answered on Stack Overflow Mar 11, 2019 by Jan Palas

User contributions licensed under CC BY-SA 3.0