title: .NET Core 3 Worker Service Settings Dependency Injection
body: <p>I have the following successfully working in .NET Core 2, but it doesn't work in .NET Core 3.</p>
  
  <p>I've made a new Worker Service .Net Core 3 project from scratch and added only the minimum to recreate my error.</p>
  
  <p>Here is my entry point in <em>Program.cs</em></p>
  
  <pre><code>namespace WorkerService1DeleteMe
  {
      public class Program
      {
          public static void Main(string[] args)
          {
              CreateHostBuilder(args).Build().Run();
          }
  
          public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
              Host.CreateDefaultBuilder(args)
                  .ConfigureServices((hostContext, services) =&gt;
                  {
                      services.Configure&lt;ConfigChunk&gt;(hostContext.Configuration.GetSection("ConfigChunk"));
                      services.AddHostedService&lt;Worker&gt;();
                  });
      }
  
      public class ConfigChunk
      {
          public string Server { get; set; }
      }
  }
  </code></pre>
  
  <p>So there is a single line added to <code>CreateHostBuilder</code> here, to the <code>Configure</code> method, and a new <code>ConfigChunk</code> class at the bottom, to match the schema of a section in <em>appsettings.json</em>.</p>
  
  <p>Finally, an extension of the parameters to the Worker constructor to let it consume the SettingsChunk:</p>
  
  <pre><code>public class Worker : BackgroundService
  {
      private readonly ILogger&lt;Worker&gt; _logger;
      private readonly ConfigChunk _config;
  
      public Worker(ILogger&lt;Worker&gt; logger, ConfigChunk config)
      {
          _logger = logger;
          _config = config;
      }
  
      protected override async Task ExecuteAsync(CancellationToken stoppingToken)
      {
          while (!stoppingToken.IsCancellationRequested)
          {
              _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
              await Task.Delay(1000, stoppingToken);
          }
      }
  }
  </code></pre>
  
  <p>As alluded to, this works fine in NET Core 2, but in 3 it fails with:</p>
  
  <blockquote>
    <p>Unable to resolve service for type 'WorkerService1DeleteMe.ConfigChunk' while attempting to activate 'WorkerService1DeleteMe.Worker'.</p>
  </blockquote>
  
  <p>I feel like I am missing something blindingly obvious but must confess I am at a loss. What I have observed is that I can make the whole thing work by going <code>services.AddSingleton</code> instead of <code>services.Configure</code> but I much prefer the pattern and reload functionality of the Configure approach if I can make it work.</p>