title: .Net Core 3.1 Worker Service use Appsettings.json
body: <p>I am trying to get my application settings out of appsettings.json for my worker service (I'm just assuming this is the correct thing to do).  I have only gotten so far but can't actually figure out how to use the appsettings in the Worker.cs</p>
  
  <p>Here is what I have in Program.cs</p>
  
  <pre><code>public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
              Host.CreateDefaultBuilder(args)
                  .ConfigureAppConfiguration((hostContext, config) =&gt;
                  {
                      // Configure the app here.
                      config
                          .SetBasePath(Environment.CurrentDirectory)
                          .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                          .AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true);
  
                      config.AddEnvironmentVariables();
                  })
                  .ConfigureServices((hostContext, services) =&gt;
                  {
                      services.AddHttpClient();
                      services.AddHostedService&lt;Worker&gt;();
                  });
  </code></pre>
  
  <p>adding worker class</p>
  
  <pre><code>public class Worker : BackgroundService
      {
          private const int ThreadDelay = 5000;
  
          private readonly ILogger&lt;Worker&gt; _logger;
          private readonly HttpClient _httpClient;
          private readonly JsonSerializer _serializer;
  
          public Worker(ILogger&lt;Worker&gt; logger, IHttpClientFactory httpClient)
          {
              _logger = logger;
              _httpClient = httpClient.CreateClient();
              _serializer = new JsonSerializer();
          }
  
          protected override async Task ExecuteAsync(CancellationToken stoppingToken)
          {
              while (!stoppingToken.IsCancellationRequested)
              {
                  _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
  
                  var locations = GetLocations(stoppingToken);
  
                  foreach(var item in locations)
                  {
  
                  }
  
  
                  //lets start the thread again in 5 minutes
                  await Task.Delay(ThreadDelay, stoppingToken);
              }
          }
  
          private string GetLocations(CancellationToken stoppingToken)
          {
              var result = string.Empty;
  
              var response = _httpClient.GetAsync($"https://", stoppingToken);
  
  
              return result;
          }
  
          private async Task TriggerPoll(CancellationToken stoppingToken)
          {
              var response = await _httpClient.GetAsync($"https://", stoppingToken);
          }
  </code></pre>