In this installment, we’ll discuss the importance of appSettings. json files in ASP.NET Core projects.

In traditional VERSIONS of ASP.NET, we stored application configuration Settings, such as database connection strings, in a web.config file. In ASP.NET Core, application configuration Settings can come from the following different configuration sources.

  • Json file (appsettings.json, appsettings.{Environment}.json) in different environments, corresponding to different hosting Environment.
  • User secrets
  • ———— launchsettings. json file
  • Command line arguments

Appsettings. json file: our project is created using the pre-made “empty” template of ASP.NET Core, so we already have an appsettings.json file in our project. We can make the following changes to the file to add a key-value pair for MyKey:

{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information"}}, "AllowedHosts": "*", "MyKey": "value of MyKey in appSettings. json"}Copy the code

Accessing configuration Information

To access configuration information in the “Startup “class, inject the IConfiguration service provided by the framework. The Startup class is in the startup. cs file.

Public class configuration {// configuration = configuration; public class configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); } app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); App. The Run (async (context) = > {/ / / / acquisition Process name / / var processName = System. Diagnostics. The Process. The GetCurrentProcess (), processName; var configVal = Configuration["MyKey"]; await context.Response.WriteAsync(configVal); }); app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); }); }}Copy the code

Dependency injection

In previous versions of ASP.NET, dependency injection was optional, and to configure it, we had to use third-party frameworks like Ninject, autofac, and castle Windsor.

Dependency injection is an integral part of ASP.NET Core. Dependency injection enables us to create systems that are low coupled, scalable, and easy to test.

Net Core IConfiguration service

  • IConfigurationThe service is designed to read configuration information from all the various configuration sources in ASP.NET Core.
  • If you have a configuration setting with the same key name in multiple configuration sources, which is simply the same name, the subsequent configuration source overwrites the previous configuration source.
  • Static classWebHosttheCreateDefaultBuilder()Method is automatically invoked at application startup to read the configuration sources in a specific order.
  • To see the read order of the configuration sources, check the following linksConfigureAppConfiguration()methodsGithub.com/aspnet/Meta…

After examining the files, you will see that the following is the default order in which the various configuration sources are read

  1. Appsettings json,
  2. appsettings.{Environment}.json
  3. The user secret
  4. The environment variable
  5. Command line arguments