public static void Run() { var builder = new ConfigurationBuilder(); builder.AddJsonFile(null, $@"C:\WorkStation\Code\GitHubCode\CoreApp\CoreWebApp\appsettings.json", true, true); var configuration = builder.Build(); Task.Run(() => { ChangeToken.OnChange(() => configuration.GetReloadToken(), () => { Console.WriteLine("Configuration has changed"); }); }); var optionsChangeTokenSource = new ConfigurationChangeTokenSource <MyOption>(configuration); var configureOptions = new NamedConfigureFromConfigurationOptions <MyOption>(string.Empty, configuration); var optionsFactory = new OptionsFactory <MyOption>(new List <IConfigureOptions <MyOption> >() { configureOptions }, new List <IPostConfigureOptions <MyOption> >()); var optionsMonitor = new OptionsMonitor <MyOption>(optionsFactory, new List <IOptionsChangeTokenSource <MyOption> >() { optionsChangeTokenSource }, new OptionsCache <MyOption>()); optionsMonitor.OnChange((option, name) => { Console.WriteLine($@"optionsMonitor Detected Configuration has changed,current Value is {option.TestOption}"); }); Thread.Sleep(600000); }
public TOptions Create(string name) { var options = CreateInstance(name); foreach (var setup in _setups) { if (setup is IConfigureNamedOptions <TOptions> namedSetup) { namedSetup.Configure(name, options); if (setup is NamedConfigureFromConfigurationOptions <TOptions> configSetup) { var target = configSetup.Action.Target; var fields = target.GetType().GetFields().Select(field => field.GetValue(target)).ToList(); var configurationSection = fields.Where(c => c is ConfigurationSection).Select(c => c as ConfigurationSection).FirstOrDefault(); if (configurationSection != null) { var tenantSection = _config.GetSection(configurationSection.Path); if (tenantSection != null) { var tenantSetup = new NamedConfigureFromConfigurationOptions <TOptions>(configSetup.Name, tenantSection); tenantSetup.Configure(name, options); } } } } else if (name == Options.DefaultName) { setup.Configure(options); } } foreach (var post in _postConfigures) { post.PostConfigure(name, options); } if (_validations != null) { var failures = new List <string>(); foreach (var validate in _validations) { var result = validate.Validate(name, options); if (result.Failed) { failures.AddRange(result.Failures); } } if (failures.Count > 0) { throw new OptionsValidationException(name, typeof(TOptions), failures); } } return(options); }