title: Integration Test for Hosted Service in .NET Core
body: <p>I have a QueueTask Hosted service (.NET Core's new background service) that I'd like to test.  My queuedHosted service looks like so:</p>
  <pre><code>public QueuedHostedService(IServiceProvider serviceProvider, IBackgroundTaskQueue taskQueue, ILoggerFactory loggerFactory)
  {
      TaskQueue = taskQueue;
      _logger = loggerFactory.CreateLogger&lt;QueuedHostedService&gt;();
      _serviceProvider = serviceProvider;
  }
  
  protected async override Task ExecuteAsync(CancellationToken stoppingToken)
  {
      using (var scope = _serviceProvider.CreateScope())
      {
          while (false == stoppingToken.IsCancellationRequested)
          {
              var workItem = await TaskQueue.DequeueAsync(stoppingToken);
              try
              {
                  await workItem(scope.ServiceProvider, stoppingToken);
              }
              catch (Exception ex)
              {
                  this._logger.LogError(ex, $&quot;Error occurred executing {nameof(workItem)}.&quot;);
              }
          }
      }
  }
  </code></pre>
  <p>It just reads tasks from the queue and executes them as they come in.  I've already verified that the Hosted Service is working in Production. I wrote a test for it like so:</p>
  <pre><code>[TestMethod]
  public async Task Verify_Hosted_Service_Executes_Task()
  {
      IServiceCollection services = new ServiceCollection();
      services.AddSingleton&lt;ILoggerFactory, NullLoggerFactory&gt;();
      services.AddHostedService&lt;QueuedHostedService&gt;();
      services.AddSingleton&lt;IBackgroundTaskQueue, BackgroundTaskQueue&gt;();
      var serviceProvider = services.BuildServiceProvider();
  
      var backgroundQueue = serviceProvider.GetService&lt;IBackgroundTaskQueue&gt;();
  
      var isExecuted = false;
      backgroundQueue.QueueBackgroundWorkItem(async (sp, ct) =&gt; {
          isExecuted = true;
      });
  
      await Task.Delay(10000);
      Assert.IsTrue(isExecuted);
  }
  </code></pre>
  <p>However, my callback is never hit.  How can I get my task to execute on the Background Service?</p>
  <p><strong>Edit</strong></p>
  <p>I was mimicking the startup, and assumed my background service would just work,
  but apparently my service is never started.</p>
  <p>How is the Hosted Service normally Started from .NET Core?</p>