title: Dependency Injection with configuration in .net core
body: <p>I am trying to inject dependency by getting configuration in class in .net core project. The class where I am trying to inject dependency is in another project. But somehow I am not able to get the values from config file in injected dependency.
  Below is my code</p>
  
  <p>In below DBContext I need to obtain value from configuration, where i have used DI of DBConfiguration class.</p>
  
  <pre><code>public class DBContext : DbContext
      {
          private readonly DBConfiguration _dBConfiguration;
  
          public DBContext(DBConfiguration dBConfiguration)
          {
              _dBConfiguration = dBConfiguration;
          }
  
          protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
          {
              optionsBuilder.UseSqlServer(_dBConfiguration.ConnectionString);
              base.OnConfiguring(optionsBuilder);
          }
  
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              base.OnModelCreating(modelBuilder);
          }
      }
  </code></pre>
  
  <p>And my StartUp.cs file in web api</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
          {
              services.AddScoped&lt;DBConfiguration&gt;();
  
              services.AddEntityFrameworkSqlServer().AddDbContext&lt;DBContext&gt;();
  
              services.AddOptions();
  
              services.Configure&lt;DBConfiguration&gt;(Configuration.GetSection("DBConfiguration"));
  
              services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
          }
  </code></pre>
  
  <p>and my appsettings.json file</p>
  
  <pre><code>{
    "DBConfiguration": {
  
  
           "ConnectionString": "Server=myserver;Database=BaseProjectDB;Trusted_Connection=True;MultipleActiveResultSets=true",
           "ApplicationName": "WebAPI"
  
  
    },
    "Logging": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "AllowedHosts": "*"
  }
  </code></pre>
  
  <p>Any help on this appreciated !</p>