title: BackgroundService not shutting down, stoppingToken never set with .net core generic host
body: <p>I have a BackgroundService hosted in .net generic host as follows:</p>
  
  <pre><code>var builder = Host.CreateDefaultBuilder(args);
  
  builder
      .ConfigureLogging((hostingContext, logging) =&gt;
      {
          logging.ClearProviders();
          logging.AddConsole();
          if(hostingContext.HostingEnvironment.IsDevelopment() == true)
              logging.AddDebug();
      })
      .ConfigureHostConfiguration(configurationBuilder =&gt;
      {
          configurationBuilder.AddCommandLine(args);
      })
      .ConfigureAppConfiguration((hostingContext, configApp) =&gt;
      {
          var env = hostingContext.HostingEnvironment;
          Console.WriteLine(env.EnvironmentName);
      })
      .UseConsoleLifetime();
  </code></pre>
  
  <p>I then have my worker:</p>
  
  <pre><code>public class Worker : BackgroundService
  {
      private readonly ILogger&lt;Worker&gt; _logger;
  
      public Worker(ILogger&lt;Worker&gt; logger)
      {
          _logger = logger;
      }
  
      protected override async Task ExecuteAsync(CancellationToken stoppingToken)
      {
          // here i am doing something with ClientWebSocket, threads etc
          // snipped for brevity. I want to avoid spinning in a loop
  
          // the debugger reaches the following line
          WaitHandle.WaitAny(new[] { stoppingToken.WaitHandle });
  
          // but never hits this line
          this.logger.LogInformation("shutting down worker");
      }
  }
  </code></pre>
  
  <p>On ctrl+c from the windows terminal of the running app it says <code>Application is shutting down</code> (this is from the framework) however the stoppingToken never gets set (so i cant shutdown my worker).</p>
  
  <p>How and when does stoppingToken get set, and how can i gracefully terminate my worker?</p>
  
  <p>The console </p>