title: DbContext in Service triggered by Hangfire
body: <p>I have a .NET 6 Razor Pages app that triggers background tasks and then informs the user of their status via SignalR.</p>
  <p>I'm trying to use Database1 context in the PerformBackgroundJob method, but it's disposed. What technique should I use to inject Database1 context in PerformBackgroundJob, or how else can I get this to work?</p>
  <pre><code>namespace Toolkat.Pages
  {
      public class ProcessModel : PageModel
      {
          private readonly Database1Context _context;
          private readonly ToolkatContext _tkcontext;
          private IConfiguration configuration;
          private readonly IQueue _queue;
          private readonly IHubContext&lt;JobHub&gt; _hubContext;
  
          static ServerConnection conn;
          static Server server;
          static Job job;
  
          public ProcessModel(
              Database1Context context,
              ToolkatContext tkcontext,
              IConfiguration _configuration,
              IQueue queue,
              IHubContext&lt;JobHub&gt; hubContext)
          {
              _context = context;
              _tkcontext = tkcontext;
              configuration = _configuration;
              _queue = queue;
              _hubContext = hubContext;
          }
  
          public IList&lt;CustomFileImport&gt; CustomFileImport { get; set; } = default!;
  
          [BindProperty]
          public CustomFileImport CustomFileImportNumberTwo { get; set; } = default!;
  
  
  
          public async Task OnGetAsync()
          {
              if (_context.CustomFileImports != null)
              {
                  CustomFileImport = await _context.CustomFileImports
                  .Include(c =&gt; c.FileImportType)
                  .Include(c =&gt; c.FileImportStatus)
                  .Where(i =&gt; i.FileImportStatusId.Equals(1))
                  .ToListAsync();
              }
          }
  
          public async Task&lt;IActionResult&gt; OnPostAsync(int[] fileImportId)
          {
              //Generate GUID
              Guid jobId = Guid.NewGuid();
  
              //Update FileImportItems with GUID
              foreach (var id in fileImportId)
              {
  
                  if (/*id == null ||*/ _context.CustomFileImports == null)
                  {
                      return NotFound();
                  }
  
                  var customfileimport = await _context.CustomFileImports.FirstOrDefaultAsync(m =&gt; m.FileImportId == id);
                  if (customfileimport == null)
                  {
                      return NotFound();
                  }
  
                  customfileimport.ProcessId = jobId;
  
                  await _context.SaveChangesAsync();
  
              }
  
  
              _queue.QueueAsyncTask(() =&gt; PerformBackgroundJob(jobId));
  
  
              return RedirectToPage(&quot;./Result&quot;, new { jobId });
          }
  
  
          private async Task PerformBackgroundJob(Guid jobId /*CancellationToken cancellationToken*/)
          {
  
              await _hubContext.Clients.Group(jobId.ToString()).SendAsync(&quot;progress&quot;, &quot;PerformBackgroundJob Started&quot;);
  
              /*
              var customFileImports = await _context.CustomFileImports
                  .Include(c =&gt; c.FileImportType)
                  .Where(i =&gt; i.ProcessId.Equals(jobId))
                  .ToListAsync();
              */
  
              Debug.WriteLine(&quot;ProviderName:&quot; + _context.Database.ProviderName);
  
              /*
              foreach (var f in customFileImports)
              {
                  await _hubContext.Clients.Group(jobId.ToString()).SendAsync(&quot;progress&quot;, WebUtility.HtmlEncode(f.FileName));
              }
              */
          }
      }
  }
  </code></pre>