asp. net core configuration file loading process

  • 2021-11-02 00:28:10
  • OfStack

Preface

The program in the configuration file plays an indispensable role in running; Normally, in the process of creating a project with visual studio, the project configuration file will be automatically generated in the root directory of the project, such as appsettings. json, or it will be widely used by everyone appsettings.{env.EnvironmentName}.json; Configuration file

As an entry point, we can intervene and adjust the program without updating the code, so it is very necessary to have a comprehensive understanding of its loading process.

When is the default configuration file loaded

In the Program. cs file, look at the following code


 public class Program
 {
  public static void Main(string[] args)
  {
   CreateWebHostBuilder(args).Build().Run();
  }

  public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
   WebHost.CreateDefaultBuilder(args)
    .UseStartup<Startup>();
 }

WebHost.CreateDefaultBuilder Located in the assembly Microsoft.AspNetCore.dll Inside, when the program executes WebHost.CreateDefaultBuilder(args) The default configuration file is loaded inside the CreateDefaultBuilder method

The code is as follows


public static IWebHostBuilder CreateDefaultBuilder(string[] args)
  {
   var builder = new WebHostBuilder();

   if (string.IsNullOrEmpty(builder.GetSetting(WebHostDefaults.ContentRootKey)))
   {
    builder.UseContentRoot(Directory.GetCurrentDirectory());
   }
   if (args != null)
   {
    builder.UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build());
   }

   builder.UseKestrel((builderContext, options) =>
    {
     options.Configure(builderContext.Configuration.GetSection("Kestrel"));
    })
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
     var env = hostingContext.HostingEnvironment;

     config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
       .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

     if (env.IsDevelopment())
     {
      var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
      if (appAssembly != null)
      {
       config.AddUserSecrets(appAssembly, optional: true);
      }
     }

     config.AddEnvironmentVariables();

     if (args != null)
     {
      config.AddCommandLine(args);
     }
    })
    .ConfigureLogging((hostingContext, logging) =>
    {
     logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
     logging.AddConsole();
     logging.AddDebug();
     logging.AddEventSourceLogger();
    })
    .ConfigureServices((hostingContext, services) =>
    {
     // Fallback
     services.PostConfigure<HostFilteringOptions>(options =>
     {
      if (options.AllowedHosts == null || options.AllowedHosts.Count == 0)
      {
       // "AllowedHosts": "localhost;127.0.0.1;[::1]"
       var hosts = hostingContext.Configuration["AllowedHosts"]?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
       // Fall back to "*" to disable.
       options.AllowedHosts = (hosts?.Length > 0 ? hosts : new[] { "*" });
      }
     });
     // Change notification
     services.AddSingleton<IOptionsChangeTokenSource<HostFilteringOptions>>(
      new ConfigurationChangeTokenSource<HostFilteringOptions>(hostingContext.Configuration));

     services.AddTransient<IStartupFilter, HostFilteringStartupFilter>();
    })
    .UseIIS()
    .UseIISIntegration()
    .UseDefaultServiceProvider((context, options) =>
    {
     options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
    });

   return builder;
  }

As you can see, the implementation of IConfigurationBuilder is still used inside CreateDefaultBuilder, and the name of the default configuration file is written dead


public static IWebHostBuilder CreateDefaultBuilder(string[] args)
  {
   var builder = new WebHostBuilder();

   if (string.IsNullOrEmpty(builder.GetSetting(WebHostDefaults.ContentRootKey)))
   {
    builder.UseContentRoot(Directory.GetCurrentDirectory());
   }
   if (args != null)
   {
    builder.UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build());
   }

   builder.UseKestrel((builderContext, options) =>
    {
     options.Configure(builderContext.Configuration.GetSection("Kestrel"));
    })
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
     var env = hostingContext.HostingEnvironment;

     config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
       .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

     if (env.IsDevelopment())
     {
      var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
      if (appAssembly != null)
      {
       config.AddUserSecrets(appAssembly, optional: true);
      }
     }

     config.AddEnvironmentVariables();

     if (args != null)
     {
      config.AddCommandLine(args);
     }
    })
    .ConfigureLogging((hostingContext, logging) =>
    {
     logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
     logging.AddConsole();
     logging.AddDebug();
     logging.AddEventSourceLogger();
    })
    .ConfigureServices((hostingContext, services) =>
    {
     // Fallback
     services.PostConfigure<HostFilteringOptions>(options =>
     {
      if (options.AllowedHosts == null || options.AllowedHosts.Count == 0)
      {
       // "AllowedHosts": "localhost;127.0.0.1;[::1]"
       var hosts = hostingContext.Configuration["AllowedHosts"]?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
       // Fall back to "*" to disable.
       options.AllowedHosts = (hosts?.Length > 0 ? hosts : new[] { "*" });
      }
     });
     // Change notification
     services.AddSingleton<IOptionsChangeTokenSource<HostFilteringOptions>>(
      new ConfigurationChangeTokenSource<HostFilteringOptions>(hostingContext.Configuration));

     services.AddTransient<IStartupFilter, HostFilteringStartupFilter>();
    })
    .UseIIS()
    .UseIISIntegration()
    .UseDefaultServiceProvider((context, options) =>
    {
     options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
    });

   return builder;
  }

Because of the above code, we can use the appsettings.json And appsettings.{env.EnvironmentName}.json The default profile name of this form

Moreover, because the Main method calls the configuration file by default, the Build method calls the configuration file


 public static void Main(string[] args)
  {
   CreateWebHostBuilder(args).Build().Run();
  }

We can get the default configuration file object IConfigurationRoot/IConfiguration using injection in Startup. cs, code snippet


 public class Startup
 {
  public Startup(IConfiguration configuration)
  {
   Configuration = configuration;
  }

This is why, because when the Build method is executed, the default configuration file object has been added to ServiceCollection inside the method, and the code fragment


 var services = new ServiceCollection();
 services.AddSingleton(_options);
 services.AddSingleton<IHostingEnvironment>(_hostingEnvironment);
 services.AddSingleton<Extensions.Hosting.IHostingEnvironment>(_hostingEnvironment);
 services.AddSingleton(_context);

 var builder = new ConfigurationBuilder()
    .SetBasePath(_hostingEnvironment.ContentRootPath)
    .AddConfiguration(_config);

 _configureAppConfigurationBuilder?.Invoke(_context, builder);

 var configuration = builder.Build();
 services.AddSingleton<IConfiguration>(configuration);
 _context.Configuration = configuration;

The above code is familiar, because in the Startup. cs file, we may have used ServiceCollection objects to add custom objects of business systems to the service context for subsequent interface injection.

The Use of AddJsonFile Method

Usually, we will use the default configuration file for development, or use the file name of appsettings. {env. EnvironmentName}. json to distinguish the development/test/production environment and load different configuration files according to the environment variables; However, this 1 brings another management problem, the configuration parameters of the product environment and the development environment

Is different, if you use environment variables to control the loading of configuration files, it may lead to risks such as password disclosure; Admittedly, this file can be created manually in the production environment, but in this way, the publishing process will become very cumbersome, and the file will be overwritten with slight errors and omissions.

We recommend using AddJsonFile to load the production environment configuration with the following code


 public Startup(IConfiguration configuration, IHostingEnvironment env)
  {
   Configuration = AddCustomizedJsonFile(env).Build();

  }

  public ConfigurationBuilder AddCustomizedJsonFile(IHostingEnvironment env)
  {
   var build = new ConfigurationBuilder();
   build.SetBasePath(env.ContentRootPath).AddJsonFile("appsettings.json", true, true);
   if (env.IsProduction())
   {
    build.AddJsonFile(Path.Combine("/data/sites/config", "appsettings.json"), true, true);
   }
   return build;
  }

Create an ConfigurationBuilder object through AddCustomizedJsonFile method, and override the default ConfigurationBuilder object of the system. In the method, the configuration file of the development environment is loaded by default, and in the product mode, the directory/data/sites/config/appsettings. json file is additionally loaded.

Different worry profile conflict, the content of the same key value will be overwritten by the configuration file added later.

Configuration file changes

When we call AddJsonFile, we see that this method has five overloaded methods

One of the methods contains four parameters, and the code is as follows


 public static IConfigurationBuilder AddJsonFile(this IConfigurationBuilder builder, IFileProvider provider, string path, bool optional, bool reloadOnChange)
  {
   if (builder == null)
   {
    throw new ArgumentNullException(nameof(builder));
   }
   if (string.IsNullOrEmpty(path))
   {
    throw new ArgumentException(Resources.Error_InvalidFilePath, nameof(path));
   }

   return builder.AddJsonFile(s =>
   {
    s.FileProvider = provider;
    s.Path = path;
    s.Optional = optional;
    s.ReloadOnChange = reloadOnChange;
    s.ResolveFileProvider();
   });
  }

In this method, there is a parameter bool reloadOnChange. From the parameter description, it can be seen that this value indicates whether to reload when the file changes. The default value is: false; 1 When manually loading the configuration file, that is, when calling the AddJsonFile method, it is recommended to set the parameter value to true.

So. netcore is if the parameter reloadOnChange is used to monitor file changes and when to reload the operation, see the following code


  public IConfigurationRoot Build()
  {
   var providers = new List<IConfigurationProvider>();
   foreach (var source in Sources)
   {
    var provider = source.Build(this);
    providers.Add(provider);
   }
   return new ConfigurationRoot(providers);
  }

When we execute the. Build method, the last line of code inside the method creates and returns an ConfigurationRoot object using the parameters of the AddJsonFile method

In the construction method of ConfigurationRoot,


  public ConfigurationRoot(IList<IConfigurationProvider> providers)
  {
   if (providers == null)
   {
    throw new ArgumentNullException(nameof(providers));
   }

   _providers = providers;
   foreach (var p in providers)
   {
    p.Load();
    ChangeToken.OnChange(() => p.GetReloadToken(), () => RaiseChanged());
   }
  }

We can see that the configuration files added by AddJsonFile method are read once inside the method, and a listener ChangeToken is assigned to each configuration file separately, and the current file reading object is bound IConfigurationProvider.GetReloadToken Method to the listener

When the file changes, the listener will receive 1 notification and perform atomic operation on the file at the same time


public static IWebHostBuilder CreateDefaultBuilder(string[] args)
  {
   var builder = new WebHostBuilder();

   if (string.IsNullOrEmpty(builder.GetSetting(WebHostDefaults.ContentRootKey)))
   {
    builder.UseContentRoot(Directory.GetCurrentDirectory());
   }
   if (args != null)
   {
    builder.UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build());
   }

   builder.UseKestrel((builderContext, options) =>
    {
     options.Configure(builderContext.Configuration.GetSection("Kestrel"));
    })
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
     var env = hostingContext.HostingEnvironment;

     config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
       .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

     if (env.IsDevelopment())
     {
      var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
      if (appAssembly != null)
      {
       config.AddUserSecrets(appAssembly, optional: true);
      }
     }

     config.AddEnvironmentVariables();

     if (args != null)
     {
      config.AddCommandLine(args);
     }
    })
    .ConfigureLogging((hostingContext, logging) =>
    {
     logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
     logging.AddConsole();
     logging.AddDebug();
     logging.AddEventSourceLogger();
    })
    .ConfigureServices((hostingContext, services) =>
    {
     // Fallback
     services.PostConfigure<HostFilteringOptions>(options =>
     {
      if (options.AllowedHosts == null || options.AllowedHosts.Count == 0)
      {
       // "AllowedHosts": "localhost;127.0.0.1;[::1]"
       var hosts = hostingContext.Configuration["AllowedHosts"]?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
       // Fall back to "*" to disable.
       options.AllowedHosts = (hosts?.Length > 0 ? hosts : new[] { "*" });
      }
     });
     // Change notification
     services.AddSingleton<IOptionsChangeTokenSource<HostFilteringOptions>>(
      new ConfigurationChangeTokenSource<HostFilteringOptions>(hostingContext.Configuration));

     services.AddTransient<IStartupFilter, HostFilteringStartupFilter>();
    })
    .UseIIS()
    .UseIISIntegration()
    .UseDefaultServiceProvider((context, options) =>
    {
     options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
    });

   return builder;
  }
0

Because the AddJsonFile method uses JsonConfigurationSource internally, and the overloaded method of Build constructs an JsonConfigurationProvider read object to view the code


public static IWebHostBuilder CreateDefaultBuilder(string[] args)
  {
   var builder = new WebHostBuilder();

   if (string.IsNullOrEmpty(builder.GetSetting(WebHostDefaults.ContentRootKey)))
   {
    builder.UseContentRoot(Directory.GetCurrentDirectory());
   }
   if (args != null)
   {
    builder.UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build());
   }

   builder.UseKestrel((builderContext, options) =>
    {
     options.Configure(builderContext.Configuration.GetSection("Kestrel"));
    })
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
     var env = hostingContext.HostingEnvironment;

     config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
       .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

     if (env.IsDevelopment())
     {
      var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
      if (appAssembly != null)
      {
       config.AddUserSecrets(appAssembly, optional: true);
      }
     }

     config.AddEnvironmentVariables();

     if (args != null)
     {
      config.AddCommandLine(args);
     }
    })
    .ConfigureLogging((hostingContext, logging) =>
    {
     logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
     logging.AddConsole();
     logging.AddDebug();
     logging.AddEventSourceLogger();
    })
    .ConfigureServices((hostingContext, services) =>
    {
     // Fallback
     services.PostConfigure<HostFilteringOptions>(options =>
     {
      if (options.AllowedHosts == null || options.AllowedHosts.Count == 0)
      {
       // "AllowedHosts": "localhost;127.0.0.1;[::1]"
       var hosts = hostingContext.Configuration["AllowedHosts"]?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
       // Fall back to "*" to disable.
       options.AllowedHosts = (hosts?.Length > 0 ? hosts : new[] { "*" });
      }
     });
     // Change notification
     services.AddSingleton<IOptionsChangeTokenSource<HostFilteringOptions>>(
      new ConfigurationChangeTokenSource<HostFilteringOptions>(hostingContext.Configuration));

     services.AddTransient<IStartupFilter, HostFilteringStartupFilter>();
    })
    .UseIIS()
    .UseIISIntegration()
    .UseDefaultServiceProvider((context, options) =>
    {
     options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
    });

   return builder;
  }
1

In JsonConfigurationProvider, it inherits from the FileConfigurationProvider class, which is located in the assembly Microsoft.Extensions.Configuration.Json.dll Inside

In the construction method of FileConfigurationProvider, the listener reloads the configuration file


public static IWebHostBuilder CreateDefaultBuilder(string[] args)
  {
   var builder = new WebHostBuilder();

   if (string.IsNullOrEmpty(builder.GetSetting(WebHostDefaults.ContentRootKey)))
   {
    builder.UseContentRoot(Directory.GetCurrentDirectory());
   }
   if (args != null)
   {
    builder.UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build());
   }

   builder.UseKestrel((builderContext, options) =>
    {
     options.Configure(builderContext.Configuration.GetSection("Kestrel"));
    })
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
     var env = hostingContext.HostingEnvironment;

     config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
       .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

     if (env.IsDevelopment())
     {
      var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
      if (appAssembly != null)
      {
       config.AddUserSecrets(appAssembly, optional: true);
      }
     }

     config.AddEnvironmentVariables();

     if (args != null)
     {
      config.AddCommandLine(args);
     }
    })
    .ConfigureLogging((hostingContext, logging) =>
    {
     logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
     logging.AddConsole();
     logging.AddDebug();
     logging.AddEventSourceLogger();
    })
    .ConfigureServices((hostingContext, services) =>
    {
     // Fallback
     services.PostConfigure<HostFilteringOptions>(options =>
     {
      if (options.AllowedHosts == null || options.AllowedHosts.Count == 0)
      {
       // "AllowedHosts": "localhost;127.0.0.1;[::1]"
       var hosts = hostingContext.Configuration["AllowedHosts"]?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
       // Fall back to "*" to disable.
       options.AllowedHosts = (hosts?.Length > 0 ? hosts : new[] { "*" });
      }
     });
     // Change notification
     services.AddSingleton<IOptionsChangeTokenSource<HostFilteringOptions>>(
      new ConfigurationChangeTokenSource<HostFilteringOptions>(hostingContext.Configuration));

     services.AddTransient<IStartupFilter, HostFilteringStartupFilter>();
    })
    .UseIIS()
    .UseIISIntegration()
    .UseDefaultServiceProvider((context, options) =>
    {
     options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
    });

   return builder;
  }
2

It is worth noting that the listener does not reload the configuration file at the first time after being notified of file changes. As you can see inside the method, there is 1 Thread.Sleep(Source.ReloadDelay) And the default value of ReloadDelay is 250ms, which is described as

Gets or sets the number of milliseconds the reload will wait, and then calls the "Load" method. This helps avoid triggering a reload before writing the file completely. The default value is 250 Thankfully, we can customize this value. If the business need for file changes is not particularly urgent, you can set this value to 1 large time, which is not usually recommended.

Conclusion

The above is the internal execution process of configuration file loading in asp. netcore, from which we realize how the default configuration file is loaded and how to inject the default configuration file into the system, and also learn the process of loading the custom configuration file if you choose in different environments; But when the configuration file changes, how does the system reload the configuration file into memory?


Related articles: