1. Make Configuration Files Take Effect Immediately
1.1 Configuration
Add reloadOnChange: true when loading configuration files in the CreateHostBuilder() method in Program.cs.
This way, when the configuration file is modified, the program will detect the file change and automatically reload it.
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, config) =>
{
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
1.2 Verification
The content of appsettings.json is as follows:
{
"TestSetting": "123",
"AppOptions": {
"UserName": "zhangsan"
}
}
Code:
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IConfiguration _configuration;
public HomeController(ILogger<HomeController> logger, IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;
}
public IActionResult Index()
{
string Name = _configuration["TestSetting"];
string Name2 = _configuration["AppOptions:UserName"];
ViewBag.Name = Name;
ViewBag.Name2 = Name2;
return View();
}
}
Interface display:

Modify the configuration file to:
{
"TestSetting": "abc",
"AppOptions": {
"UserName": "zhangsan123"
}
}
Refresh the page; the changes have taken effect:

1.3 IOptions Approach for Real-Time Effect
Create a new AppOptions.cs class:
/// <summary>
/// Configuration options
/// </summary>
public class AppOptions
{
public string UserName { get; set; }
}
Add the configuration to Options in Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.Configure<AppOptions>(Configuration.GetSection("AppOptions"));
}
Usage:
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IConfiguration _configuration;
private IOptionsMonitor<AppOptions> _options;
public HomeController(ILogger<HomeController> logger, IConfiguration configuration, IOptionsMonitor<AppOptions> appOptions)
{
_logger = logger;
_configuration = configuration;
_options = appOptions;
}
public IActionResult Index()
{
string Name = _configuration["TestSetting"];
string Name2 = _options.CurrentValue.UserName;
ViewBag.Name = Name;
ViewBag.Name2 = Name2;
return View();
}
}
There are three types of IOptions:
1. IOptions<T> // After the site starts, the retrieved value never changes
2. IOptionsMonitor<T> // After the site starts, if the configuration file changes, an event is published (reloadOnChange: true must be true when loading the configuration)
3. IOptionsSnapshot<T> // After the site starts, each retrieval gets the latest value from the configuration file (reloadOnChange: true must be true when loading the configuration)
Note:
The biggest difference between
IOptionsMonitor<T>andIOptionsSnapshot<T>is that the former can be used by other singleton services while the latter cannot. This is because the former is registered as a singleton and the latter as scoped. That means when a file is modified, the former reloads immediately, while the latter only reloads per request.
Example:
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private UserService _userService;
public HomeController(ILogger<HomeController> logger, UserService userService)
{
_userService = userService;
}
public IActionResult Index()
{
string Name2 = _userService.GetName();
ViewBag.Name2 = Name2;
return View();
}
}
public class UserService
{
private IOptionsMonitor<AppOptions> _options;
public UserService(IOptionsMonitor<AppOptions> appOptions)
{
_options = appOptions;
}
public string GetName()
{
var Name = _options.CurrentValue.UserName;
return Name;
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.Configure<AppOptions>(Configuration.GetSection("AppOptions"));
services.AddSingleton<UserService>();
}
In the above example, UserService is injected as a singleton. Using IOptionsMonitor<T> allows real-time configuration refresh, while using IOptionsSnapshot<T> will cause an error at startup.
1.4 Multiple Configuration Files Taking Effect Immediately
Add an additional db configuration file:

Modify CreateHostBuilder() in Program.cs; just add reloadOnChange: true when loading.
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, config) =>
{
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
config.AddJsonFile("Configs/dbsetting.json", optional: true, reloadOnChange: true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
Usage is the same:
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IConfiguration _configuration;
private AppOptions _options;
public HomeController(ILogger<HomeController> logger, IConfiguration configuration, IOptionsMonitor<AppOptions> appOptions)
{
_logger = logger;
_configuration = configuration;
_options = appOptions.CurrentValue;
}
public IActionResult Index()
{
string Name = _configuration["TestSetting"];
string Name2 = _configuration["db:connection1"];
ViewBag.Name = Name;
ViewBag.Name2 = Name2;
return View();
}
}