title: .Net Core How to Access Configuration Anywhere in application
body: <p>I have read through the documentation on the different ways to setup and access configuration in .Net Core 2.1 and also the options pattern that seems to be recommended (<a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-2.1" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-2.1</a>).  However, I can't seem to get what I want working:</p>
  
  <p>I have done the following:</p>
  
  <p>AppSettings:</p>
  
  <pre><code>{
    "ConnectionStrings": {
      "DefaultConnStr": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true;Integrated Security=true",
      "AW2012ConnStr": "Server=localhost;Database=AW2012;Trusted_Connection=True;MultipleActiveResultSets=true;Integrated Security=true"
    }
  }
  </code></pre>
  
  <p>MyConfig:</p>
  
  <pre><code>public class MyConfig
  {
      public string AWConnStr { get; }
      public string DefaultConnStr { get; }
  }
  </code></pre>
  
  <p>Startup: </p>
  
  <pre><code>public class Startup
  {
  
  public IConfiguration _config { get; set; }
  
  public Startup(IHostingEnvironment env)
  {
       var builder = new ConfigurationBuilder()
                  .SetBasePath(env.ContentRootPath)
                  .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
       _config = builder.Build();
  
  }
  
  public void ConfigureServices(IServiceCollection services)
  {
        services.AddOptions();
  
        //add config to services for dependency injection
        //services.AddTransient&lt;IMyConfig, MyConfig&gt;();
       //services.AddScoped&lt;IMyConfig, MyConfig&gt;();
       var section = _config.GetSection("ConnectionStrings");
       services.Configure&lt;MyConfig&gt;(section);
  }
  
      private static void HandleGetData(IApplicationBuilder app)
      {
          //DataHelper dataHelper = new DataHelper(_dataHelper);
          var _dataHelper = app.ApplicationServices.GetService&lt;DataHelper&gt;();
  
          app.Run(async context =&gt;
          {
              //await context.Response.WriteAsync("&lt;b&gt;Get Data&lt;/b&gt;");
              //await context.Response.WriteAsync(dataHelper.GetCompetitions(context.Request.QueryString.ToString()));
              await context.Response.WriteAsync(_dataHelper.GetCompetitions(context.Request.QueryString.ToString()));
          });
      }
  
  
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
          {
  
              app.Map("/Route1", HandleRoute1);
  
              app.Map("/Route2", HandleRoute2);
  
              app.Map("/GetData", HandleGetData);
  
              app.Run(async (context) =&gt;
              {
                  await context.Response.WriteAsync("Non Mapped Default");
              });
          }
   }
  </code></pre>
  
  <p>I would like to then access the configuration in any class anywhere in my code. So for example I have the following class where I would like to just read the configuration information:</p>
  
  <pre><code>public interface IDataHelper
  {
      string GetCompetitions(string val);
  }
  
  public class DataHelper : IDataHelper
  {
      private readonly MyConfig _settings;
  
      public DataHelper(IOptions&lt;MyConfig&gt; options)
      {
          _settings = options.Value;
      }
  
      public string GetCompetitions( string queryStringVals)
      {
  
          return _settings.AWConnStr;
  
      } 
  }
  </code></pre>
  
  <p>As shown above in my Startup class I then want to access/call something in the HandleGetData function in my startup, so that when I browse to the following route:  <a href="http://localhost:xxxxx/getdata" rel="nofollow noreferrer">http://localhost:xxxxx/getdata</a> I get back the response from the Something.GetData function.</p>
  
  <p>Is this correct? The problem I'm having is that when I create an instance of class Something, it is requiring me to pass in the configuration object, but doesn't that defeat the purpose of injecting it.  How should I be setting this up to work similar to how DBContext gets the context injected with the configuration options.  And what's the difference between services.AddTransient and services.AddScoped?  I've seen both as a way to register the service.</p>