public void AutoWiredConfiguredOptionsDoNotBreakWhenChangingConfiguration() { var config = GetConfiguration(); var sc = new ServiceCollection(); sc.AutoWire(config, autoWireOptions => { autoWireOptions.EntryAssembly = typeof(AutoWireTests).Assembly; autoWireOptions.ReloadConfigurationInterval = TimeSpan.FromSeconds(1); autoWireOptions.MakeOptionsMonitorUpdateSafe = true; autoWireOptions.OnOptionsMonitorUpdateException = (o, type, ex) => { Assert.NotNull(o); }; }); var sp = sc.BuildServiceProvider(); var optionsMonitor = sp.GetRequiredService <IOptionsMonitor <ConfigOptions> >(); Assert.True(optionsMonitor.CurrentValue.Key4); Environment.SetEnvironmentVariable("CONFIGOPTIONS__KEY4", "invalid_key_value_because_is_not_boolean"); var backgroundService = new ConfigReloaderBackgroundService(config, sp.GetRequiredService <AutoWireOptions>(), new NullLogger <ConfigReloaderBackgroundService>()); backgroundService.StartAsync(new CancellationToken()); Thread.Sleep(5000); optionsMonitor = sp.GetRequiredService <IOptionsMonitor <ConfigOptions> >(); Assert.True(optionsMonitor.CurrentValue.Key4); }
public void AutoWireConfiguresOptions() { var config = GetConfiguration(); var sc = new ServiceCollection(); sc.AutoWire(config, autoWireOptions => { autoWireOptions.EntryAssembly = typeof(AutoWireTests).Assembly; autoWireOptions.MakeOptionsMonitorUpdateSafe = true; autoWireOptions.ReloadConfigurationInterval = TimeSpan.FromSeconds(1); }); var sp = sc.BuildServiceProvider(); var options = sp.GetRequiredService <IOptions <ConfigOptions> >(); var optionsMonitor = sp.GetRequiredService <IOptionsMonitor <ConfigOptions> >(); optionsMonitor.OnChange((configOptions, s) => { Assert.NotNull(configOptions); }); Assert.NotNull(options.Value.Key1); Assert.NotNull(optionsMonitor.CurrentValue.Key1); Environment.SetEnvironmentVariable("CONFIGOPTIONS__KEY1", "replaced_value"); var backgroundService = new ConfigReloaderBackgroundService(config, sp.GetRequiredService <AutoWireOptions>(), new NullLogger <ConfigReloaderBackgroundService>()); backgroundService.StartAsync(new CancellationToken()); Thread.Sleep(5000); options = sp.GetRequiredService <IOptions <ConfigOptions> >(); optionsMonitor = sp.GetRequiredService <IOptionsMonitor <ConfigOptions> >(); Assert.Equal("value1", options.Value.Key1); Assert.Equal("replaced_value", optionsMonitor.CurrentValue.Key1); var optionsValue = options.Value; Assert.NotNull(optionsValue); Assert.Equal("value1", optionsValue.Key1); Assert.Equal("value2", optionsValue.Key2); Assert.Equal(1, optionsValue.Key3); Assert.True(optionsValue.Key4); Assert.NotNull(optionsValue.SubKey); Assert.Contains(optionsValue.SubKey.Key2, x => x == "value2.1"); }