title: Add appsettings configuration DI to HostBuilder console app
body: <p>I am trying to include an <em>appsettings.json</em> file in a .NET Core console app where I am using <code>HostBuilder</code>, and attempting to add a section (AppVars) to the configuration. When I try to do this, I get the error:</p>
  <blockquote>
  <p>System.InvalidOperationException: Unable to resolve service for type AppVars while attempting to activate...</p>
  </blockquote>
  <p>Here is my code:</p>
  <pre><code>private static async Task Main(string[] args)
  {
      var isService = !(Debugger.IsAttached || args.Contains(&quot;--console&quot;));
  
      var config1 = new ConfigurationBuilder()
          .SetBasePath(Directory.GetCurrentDirectory())
          .AddJsonFile(&quot;appsettings.json&quot;, optional: true)
          .Build();
  
      var builder = new HostBuilder()
          .ConfigureServices((hostContext, services) =&gt;
          {
              services.AddHostedService&lt;FileWriterService&gt;();
  
              services.Configure&lt;AppVars&gt;(config1.GetSection(&quot;AppVars&quot;));
  
          });
  
      //...
  }
  </code></pre>
  <p>And below I am trying to Inject AppVars in a class:</p>
  <pre><code>public class FileWriterService : IHostedService, IDisposable
  {
      private const string Path = @&quot;d:\TestApplication.txt&quot;;
  
      private Timer _timer;
      private AppVars _appVars;
      public FileWriterService(AppVars appVars)
      {
         //_appVars = appVars;
      }
  
  
      //...
  
  }
  </code></pre>