Actually read AppSettings in the ConfigureServices phase in ASP.NET Core

I need to configure several dependencies (services) in the ConfigureServices method in an ASP.NET Core 1.0 web application.

The problem is that based on the new JSON configuration, I need to configure a service or another.

It seems that I really did not read the settings in the ConfigureServices phase of the application lifetime:

 public void ConfigureServices(IServiceCollection services) { var section = Configuration.GetSection("MySettings"); // this does not actually hold the settings services.Configure<MySettingsClass>(section); // this is a setup instruction, I can't actually get a MySettingsClass instance with the settings // ... // set up services services.AddSingleton(typeof(ISomething), typeof(ConcreteSomething)); } 

I would really need to read this section and decide what to register for ISomething (maybe a different type than ConcreteSomething ).

+18
source share
3 answers

In this way, you can get the entered settings from appSettings.json directly in the ConfigureServices method:

 public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.Configure<MySettings>(Configuration.GetSection(nameof(MySettings))); services.AddSingleton(Configuration); // ... var settings = Configuration.GetSection(nameof(MySettings)).Get<MySettings>(); int maxNumberOfSomething = settings.MaxNumberOfSomething; // ... } // ... } 
+26
source

Starting with ASP.NET Core 2.0, we perform configuration in the Program class when building the WebHost instance. An example of such a setting:

 return new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .ConfigureAppConfiguration((builderContext, config) => { IHostingEnvironment env = builderContext.HostingEnvironment; config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); }) 

Among other things, this allows you to use the configuration directly in the Startup class, getting an instance of IConfiguration using the constructor injector (thanks, built-in DI container):

 public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } ... } 
+10
source

You can access the values โ€‹โ€‹of appsettings.json using Configuration["ConfigSection:ConfigValue"])

 public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddDbContext<MyContext>(o => o.UseSqlServer(Configuration["AppSettings:SqlConn"])); } } 

appsettings.json

 { "Logging": { "LogLevel": { "Default": "Warning", "System": "Information", "Microsoft": "Warning" } }, "AppSettings": { "SqlConn": "Data Source=MyServer\\MyInstance;Initial Catalog=MyDb;User ID=sa;Password=password;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;" } } 
+1
source

Source: https://habr.com/ru/post/1012116/


All Articles