title: Cannot access a disposed object (on DbContext) error on Hangfire recurrent job in ABP
body: <p>I am trying to set up a Hangfire recurrent job on application startup, but in the job if I try to use an injected <code>IRepository</code>, it gives an error related to the DbContext when it tries to execute a query:</p>
  
  <pre><code>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: 'WsApplicationDbContext'.
  
  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: 'WsApplicationDbContext'.
     at Microsoft.EntityFrameworkCore.DbContext.CheckDisposed()
     at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()
     at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable()
     at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Linq.IQueryable.get_Provider()
     at System.Linq.Queryable.Select[TSource,TResult](IQueryable`1 source, Expression`1 selector)
     at WsApplication.RecurringJobs.Jobs.ContainerIsBookedOffHireRecurringJob.TestJob() in C:\Projects\boxman\aspnet-core\src\WsApplication.Application\RecurringJobs\Jobs\ContainerIsBookedOffHireRecurringJob.cs:line 26
  </code></pre>
  
  <p>I've registered Hangfire exactly as it is described in the <a href="https://aspnetboilerplate.com/Pages/Documents/Hangfire-Integration" rel="nofollow noreferrer">ABP documentation</a>.
  In addition I am registering my job like this in the <code>&lt;ApplicationName&gt;.Web.Core</code> module:</p>
  
  <pre><code>public override void PostInitialize()
  {
      ...
  
      // set up recurring background jobs
      var job = IocManager.Resolve&lt;ContainerIsBookedOffHireRecurringJob&gt;();
      RecurringJob.AddOrUpdate&lt;ContainerIsBookedOffHireRecurringJob&gt;("TestJobWithRepository", j =&gt; j.TestJob(), Cron.Daily);
  }
  </code></pre>
  
  <p>And my job method is simply trying to do a query with an injected repository.</p>
  
  <pre><code>public class ContainerIsBookedOffHireRecurringJob : BaseRecurringJob
  {
      private readonly IRepository&lt;ContractLineMovement, int&gt; _contractLineMovementRepository;
  
      public ContainerIsBookedOffHireRecurringJob(
          IIocResolver iocResolver,
          IRepository&lt;ContractLineMovement, int&gt; contractLineMovementRepository
      ) : base(iocResolver)
      {
          _contractLineMovementRepository = contractLineMovementRepository;
      }
  
      public void TestJob()
      {
          var testQuery = from contractLineMovement in _contractLineMovementRepository.GetAll()
                          select contractLineMovement;
  
          testQuery.ToList().ForEach(t =&gt;
          {
              Logger.Debug("Fetched ContractLineMovement ID: " + t.Id + " Type: " + t.ContractWorkFlowEventId);
          });
      }
  }
  </code></pre>
  
  <p>I've managed to work around it by resolving a <code>WsApplicationDbContext</code> directly in the <code>BaseRecurringJob</code> but I don't really like that solution. I don't understand why it doesn't simply work with dependency injection.
  Is it because I'm registering them at startup? Do I need some kind of custom Job Activator?</p>
  
  <p>Any help would be much appreciated.</p>