public EgressService( IServiceProvider serviceProvider, ILogger <EgressService> logger, IConfiguration configuration, IEnumerable <IEgressProviderConfigurationProvider> providers) { _logger = logger; _providers = providers; _providerTypeMap = new ConcurrentDictionary <string, string>(StringComparer.OrdinalIgnoreCase); _optionsTypeMap = new ConcurrentDictionary <string, Type>(StringComparer.OrdinalIgnoreCase); _serviceProvider = serviceProvider; _changeRegistration = ChangeToken.OnChange( () => configuration.GetEgressSection().GetReloadToken(), Reload); Reload(); }
/// <summary> /// ConfigInit /// </summary> /// <param name="jsonName"></param> /// <param name="reloadOnChange"></param> public JsonConfiguration(string jsonName, bool reloadOnChange = true) { Configuration = new ConfigurationBuilder() .Add(new JsonConfigurationSource { Path = $"{jsonName}.json", ReloadOnChange = reloadOnChange }) .Build(); ChangeToken.OnChange(() => Configuration.GetReloadToken(), () => { var now = DateTime.Now.ToTimeStamp(); if ((now - ChangeTime) <= Time) { return; } ChangeTime = now; OnChange?.Invoke(); }); }
public void Run() { var formatOptions = MyConfiguration.Root.GetSection("format") .Get <FormatOptions>(); Console.WriteLine(JsonSerializer.Serialize(formatOptions).AsFormatJsonStr()); ChangeToken.OnChange(() => MyConfiguration.Root.GetReloadToken(), () => { Console.WriteLine("触发配置变更"); /** * 当对应的配置文件的reloadOnChange为true,当文件发生变更时,会触发 * 注意:这里只需要对变更做处理,不需要再重新从文件build,系统会自动更新IConfigurationRoot,即进入这里的时候,IConfigurationRoot已经是同步后的了。 * p.s.测试修改配置文件的时候,使用notepad编辑,会出现触发多次的情况,这是notepad的原因,使用默认文本编辑器并不会,很坑 */ }); }
public static IApplicationBuilder ExecuteStartupTasks(this IApplicationBuilder app) { var config = app.GetRequiredService <IConfiguration>(); var botService = app.GetRequiredService <BotService>(); app.GetRequiredService <DatabaseService>().FixTableCollation().WaitAndUnwrapException(); botService.EnsureCommandRegistration().WaitAndUnwrapException(); botService.SendStartupNotification().WaitAndUnwrapException(); app.RunMongoDbPreparation(); ChangeToken.OnChange( () => config.GetReloadToken(), () => botService.EnsureCommandRegistration().WaitAndUnwrapException() ); return(app); }