监听配置的变更:
static void Main(string[] args)
{
var builder = new ConfigurationBuilder();
builder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
var configurationRoot = builder.Build();
ChangeToken.OnChange(() => configurationRoot.GetReloadToken(), () =>
{
Console.WriteLine($"Key1:{configurationRoot["Key1"]}");
Console.WriteLine($"Key2:{configurationRoot["Key2"]}");
Console.WriteLine($"Key3:{configurationRoot["Key3"]}");
});
Console.WriteLine("开始了");
Console.ReadKey();
}