title: Calling appsettings.json data from another class in a console app
body: <p>The following is an example code from <a href="https://docs.microsoft.com/en-us/dotnet/core/extensions/configuration-providers#file-configuration-provider" rel="nofollow noreferrer">Microsoft documentation which shows how to use appsettings.json in console application</a>:</p>
  <pre><code>using System;
  using System.Threading.Tasks;
  using Microsoft.Extensions.Configuration;
  using Microsoft.Extensions.Hosting;
  
  namespace ConsoleJson.Example
  {
      class Program
      {
          static async Task Main(string[] args)
          {
              using IHost host = CreateHostBuilder(args).Build();
  
              // Application code should start here.
  
              await host.RunAsync();
          }
  
          static IHostBuilder CreateHostBuilder(string[] args) =&gt;
              Host.CreateDefaultBuilder(args)
                  .ConfigureAppConfiguration((hostingContext, configuration) =&gt;
                  {
                      configuration.Sources.Clear();
  
                      IHostEnvironment env = hostingContext.HostingEnvironment;
  
                      configuration
                          .AddJsonFile(&quot;appsettings.json&quot;, optional: true, reloadOnChange: true)
                          .AddJsonFile($&quot;appsettings.{env.EnvironmentName}.json&quot;, true, true);
  
                      IConfigurationRoot configurationRoot = configuration.Build();
  
                      TransientFaultHandlingOptions options = new();
                      configurationRoot.GetSection(nameof(TransientFaultHandlingOptions))
                                       .Bind(options);
  
                      Console.WriteLine($&quot;TransientFaultHandlingOptions.Enabled={options.Enabled}&quot;);
                      Console.WriteLine($&quot;TransientFaultHandlingOptions.AutoRetryDelay={options.AutoRetryDelay}&quot;);
                  });
      }
  }
  </code></pre>
  <p>I need to get the configuration values in a method in <strong>another class</strong> as below. I think I will need to use <strong>Dependency injection</strong> and send <code>IConfigurationRoot</code> to <code>myClass</code> as shown in <a href="https://docs.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-usage" rel="nofollow noreferrer">this link</a> but can not figure out exactly how??</p>
  <pre><code>using System;
  using System.Threading.Tasks;
  using Microsoft.Extensions.Configuration;
  using Microsoft.Extensions.Hosting;
  
  namespace ConsoleJson.Example
  {
      class Program
      {
          static void Main(string[] args)
          {
              using IHost host = CreateHostBuilder(args).Build();
  
              IDatabase db = xyz.GetDatabase();
  
              ConfigureServices.Initialize(db);
          }
  
          static IHostBuilder CreateHostBuilder(string[] args) =&gt;
              Host.CreateDefaultBuilder(args)
                  .ConfigureAppConfiguration((hostingContext, configuration) =&gt;
                  {
                      configuration.Sources.Clear();
  
                      IHostEnvironment env = hostingContext.HostingEnvironment;
  
                      configuration
                          .AddJsonFile(&quot;appsettings.json&quot;, optional: true, reloadOnChange: true)
                          .AddJsonFile($&quot;appsettings.{env.EnvironmentName}.json&quot;, true, true);
  
                      IConfigurationRoot configurationRoot = configuration.Build();
  
                      TransientFaultHandlingOptions options = new();
                      configurationRoot.GetSection(nameof(TransientFaultHandlingOptions))
                                       .Bind(options);
  
                      
                  });
      }
  
      class ConfigureServices
      {   public static void Initialize(IDatabase db)
          {
               Console.WriteLine($&quot;TransientFaultHandlingOptions.Enabled={options.Enabled}&quot;);
               Console.WriteLine($&quot;TransientFaultHandlingOptions.AutoRetryDelay={options.AutoRetryDelay}&quot;);
  
          }
      }
  }
  </code></pre>
  <p><strong>So, I need to get the values from <code>appsettings.json</code> and use them in my Initialize method. I'm able to use the rest of the code by hardcoding the values.</strong></p>
  <p>I tried adding the below line after ConfigureAppConfiguration but it's not able to recognize <code>configurationRoot</code></p>
  <pre><code>.ConfigureServices((_, services) =&gt;services.AddSingleton&lt;IConfigurationRoot&gt;(configurationRoot));
  </code></pre>
  <p>All help is sincerely appreciated.
  Thanks</p>