title: .Net Core Hosted Service Requires HttpContext
body: <p><strong>EDIT Summary</strong></p>
  
  <p>I have a background service, which requires a DBContext, my DbContext is dependent on the HttPContext, because it uses the UserClaims to filter the context.  The HttpContext which is null when requesting the DbContext from BackgroundService, (this is by design because there is no WebRequest associated with a BackgroundService). </p>
  
  <p>How can I capture and moq the HTTPContext, so my user claims will carry over to the background service?</p>
  
  <p>I'm writing a hosted service to queue pushing messages from my Hubs in Signalr.  My Background.</p>
  
  <pre><code>public interface IBackgroundTaskQueue
  {
      void QueueBackgroundWorkItem(Func&lt;IServiceProvider, CancellationToken, Task&gt; workItem);
      Task&lt;Func&lt;IServiceProvider, CancellationToken, Task&gt;&gt; DequeueAsync(CancellationToken cancellationToken);
  }
  
  public class BackgroundTaskQueue : IBackgroundTaskQueue
  {
      private ConcurrentQueue&lt;Func&lt;IServiceProvider, CancellationToken, Task&gt;&gt; _workItems = new ConcurrentQueue&lt;Func&lt;IServiceProvider, CancellationToken, Task&gt;&gt;();
      private SemaphoreSlim _signal = new SemaphoreSlim(0);
  
      public void QueueBackgroundWorkItem(Func&lt;IServiceProvider, CancellationToken, Task&gt; workItem)
      {
          if (workItem == null)
          {
              throw new ArgumentNullException(nameof(workItem));
          }
  
          this._workItems.Enqueue(workItem);
          this._signal.Release();
      }
  
      public async Task&lt;Func&lt;IServiceProvider, CancellationToken, Task&gt;&gt; DequeueAsync(
          CancellationToken cancellationToken)
      {
          await this._signal.WaitAsync(cancellationToken);
          this._workItems.TryDequeue(out var workItem);
  
          return workItem;
      }
  }
  </code></pre>
  
  <p>I'm queue work items that require my DbContext,  My Db Context automatically filters data based off the logged in user, So it requires access to the HTTPContextAccessor.</p>
  
  <p>When I Queue up an Item that requires my DbContext, I get an error the the HTTPContext is null, which makes sense because, I'm running in a background process.</p>
  
  <pre><code>var backgroundTask = sp.GetRequiredService&lt;IBackgroundTaskQueue&gt;();
  
  backgroundTask.QueueBackgroundWorkItem((isp, ct) =&gt; {
      //When this line is executed on the background task it throws
      var context = sp.GetService&lt;CustomDbContext&gt;(); 
      //... Do work with context
  });
  </code></pre>
  
  <p>My DBContext uses the HTTPContextAccessor to filter data:</p>
  
  <pre><code>public CustomDbContext(DbContextOptions options, IHttpContextAccessor httpContextAccessor)
  {
  }
  </code></pre>
  
  <p>Is there a way I can capture the HTTPContext with each Task, or perhaps Moq one and capture the UserClaims, and replace them in the scope for the BackgroundService?</p>
  
  <p>How can I use services dependent on HTTPContext from my background services.</p>