protected override void OnStart(string[] args) { try { var temp = new ConfigurationBuilder() .AddJsonFile("config.json") .AddJsonFile("hosting.json", true) .Build(); var configProvider = new MemoryConfigurationProvider(); configProvider.Add("server.urls", temp["WebServerAddress"]); configProvider.Add("webroot", temp.Get <string>("webroot", "wwwroot")); var config = new ConfigurationBuilder() .Add(configProvider) .Build(); var builder = new WebHostBuilder(config); builder.UseServer("Microsoft.AspNet.Server.Kestrel"); builder.UseStartup <Startup>(); var hostingEngine = builder.Build(); _application = hostingEngine.Start(); } catch (Exception ex) { Debug.WriteLine("error in OnStart: " + ex); throw; } }
protected override void OnStart(string[] args) { try { var configProvider = new MemoryConfigurationProvider(); configProvider.Add("server.urls", "http://localhost:5000"); var config = new ConfigurationBuilder() .Add(configProvider) .Build(); var builder = new WebHostBuilder(config); builder.UseServer("Microsoft.AspNet.Server.Kestrel"); builder.UseServices(services => services.AddMvc()); builder.UseStartup(appBuilder => { appBuilder.UseDefaultFiles(); appBuilder.UseStaticFiles(); appBuilder.UseMvc(); }); var hostingEngine = builder.Build(); _application = hostingEngine.Start(); } catch (Exception ex) { Debug.WriteLine("error in OnStart: " + ex); throw; } }
public void BasicSetGetTest() { var provider = new MemoryConfigurationProvider(); CommonTestRunner commonTests = new CommonTestRunner(provider); commonTests.RunBasicSetGetTest(); }
public void ReadUnsetOptionsWithDefaultValueTest() { var provider = new MemoryConfigurationProvider(); CommonTestRunner commonTests = new CommonTestRunner(provider); commonTests.RunReadUnsetOptionsWithDefaultValueTest(); }
internal static void Main() { var configSource = new MemoryConfigurationSource { InitialData = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("env", "Development"), new KeyValuePair <string, string>("host", "localhost"), new KeyValuePair <string, string>("port", "9000"), } }; var configProvider = new MemoryConfigurationProvider(new MemoryConfigurationSource { InitialData = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("env", "Production") } }); var configProviders = new List <IConfigurationProvider> { configSource.Build(null), // like configProvider above configProvider }; var configRoot = new ConfigurationRoot(configProviders); Console.WriteLine($"{configRoot["env"]}|{configRoot["host"]}|{configRoot["port"]}"); }
public static IConfigurationBuilder AddApplicationInsightsSettings(this IConfigurationBuilder configurationSourceRoot, bool?developerMode = null, string endpointAddress = null, string instrumentationKey = null) { var telemetryConfigurationSource = new MemoryConfigurationProvider(); bool wasAnythingSet = false; if (developerMode != null) { telemetryConfigurationSource.Set(DeveloperModeForWebSites, developerMode.Value.ToString()); wasAnythingSet = true; } if (instrumentationKey != null) { telemetryConfigurationSource.Set(InstrumentationKeyForWebSites, instrumentationKey); wasAnythingSet = true; } if (endpointAddress != null) { telemetryConfigurationSource.Set(EndpointAddressForWebSites, endpointAddress); wasAnythingSet = true; } if (wasAnythingSet) { configurationSourceRoot.Add(telemetryConfigurationSource); } return(configurationSourceRoot); }
public void NewConfigurationProviderOverridesOldOneWhenKeyIsDuplicated() { // Arrange var dic1 = new Dictionary <string, string>() { { "Key1:Key2", "ValueInMem1" } }; var dic2 = new Dictionary <string, string>() { { "Key1:Key2", "ValueInMem2" } }; var memConfigSrc1 = new MemoryConfigurationProvider(dic1); var memConfigSrc2 = new MemoryConfigurationProvider(dic2); var builder = new ConfigurationBuilder(); // Act builder.Add(memConfigSrc1, load: false); builder.Add(memConfigSrc2, load: false); var config = builder.Build(); // Assert Assert.Equal("ValueInMem2", config["Key1:Key2"]); }
public void SettingValueUpdatesAllConfigurationProviders() { // Arrange var dict = new Dictionary <string, string>() { { "Key1", "Value1" }, { "Key2", "Value2" } }; var memConfigSrc1 = new MemoryConfigurationProvider(dict); var memConfigSrc2 = new MemoryConfigurationProvider(dict); var memConfigSrc3 = new MemoryConfigurationProvider(dict); var builder = new ConfigurationBuilder(); builder.Add(memConfigSrc1, load: false); builder.Add(memConfigSrc2, load: false); builder.Add(memConfigSrc3, load: false); var config = builder.Build(); // Act config["Key1"] = "NewValue1"; config["Key2"] = "NewValue2"; // Assert Assert.Equal("NewValue1", config["Key1"]); Assert.Equal("NewValue1", memConfigSrc1.Get("Key1")); Assert.Equal("NewValue1", memConfigSrc2.Get("Key1")); Assert.Equal("NewValue1", memConfigSrc3.Get("Key1")); Assert.Equal("NewValue2", config["Key2"]); Assert.Equal("NewValue2", memConfigSrc1.Get("Key2")); Assert.Equal("NewValue2", memConfigSrc2.Get("Key2")); Assert.Equal("NewValue2", memConfigSrc3.Get("Key2")); }
public void SourcesReturnsAddedConfigurationProviders() { // Arrange var dict = new Dictionary <string, string>() { { "Mem:KeyInMem", "MemVal" } }; var memConfigSrc1 = new MemoryConfigurationProvider(dict); var memConfigSrc2 = new MemoryConfigurationProvider(dict); var memConfigSrc3 = new MemoryConfigurationProvider(dict); var srcSet = new HashSet <IConfigurationProvider>() { memConfigSrc1, memConfigSrc2, memConfigSrc3 }; var builder = new ConfigurationBuilder(); // Act builder.Add(memConfigSrc1, load: false); builder.Add(memConfigSrc2, load: false); builder.Add(memConfigSrc3, load: false); var config = builder.Build(); // Assert Assert.Equal(new[] { memConfigSrc1, memConfigSrc2, memConfigSrc3 }, builder.Providers); }
public void ReadsConfigurationFromIConfiguration() { var provider = new MemoryConfigurationProvider(new MemoryConfigurationSource()) { { "test2", "2" }, { "Logging:LogLevel:Microsoft.Hosting.Lifetime", "3" }, }; using var configurationRoot = new ConfigurationRoot(new List <IConfigurationProvider>() { provider, }); var sut = new EnvironmentProvider(configurationRoot, null); var result = sut.ReadConfiguration(); result.Should().NotBeNull(); result.Should().HaveCount(2); // 2 because there is the default PID Provider of Net Core Admin var prov = result.First(); prov.Name.Should().Be("MemoryConfigurationProvider - 0"); prov.Properties.Should().HaveCount(2); prov.Properties.Keys.Should().Contain("test2"); prov.Properties.Values.Should().ContainEquivalentOf(new PropertyValue("2")); prov.Properties.Keys.Should().Contain("Logging:LogLevel:Microsoft.Hosting.Lifetime"); prov.Properties.Values.Should().ContainEquivalentOf(new PropertyValue("3")); }
public void GetEnvironmentDataIncludesEnvironmentName() { var envName = "TEST"; var hostingEnv = Mock.Of <IWebHostEnvironment>(); hostingEnv.EnvironmentName = envName; var firstProvider = new MemoryConfigurationProvider(new MemoryConfigurationSource()) { { "test", "test" }, }; var secondProvider = new MemoryConfigurationProvider(new MemoryConfigurationSource()) { { "test2", "2" }, { "Logging:LogLevel:Microsoft.Hosting.Lifetime", "2" }, }; using var configurationRoot = new ConfigurationRoot(new List <IConfigurationProvider>() { firstProvider, secondProvider, }); var sut = new EnvironmentProvider(configurationRoot, hostingEnv); var data = sut.GetEnvironmentData(); data.ActiveProfiles.Should().Contain(envName); }
public void ReadsConfigurationFromTwoSources() { var firstProvider = new MemoryConfigurationProvider(new MemoryConfigurationSource()) { { "test", "test" }, }; var secondProvider = new MemoryConfigurationProvider(new MemoryConfigurationSource()) { { "test2", "2" }, { "Logging:LogLevel:Microsoft.Hosting.Lifetime", "2" }, }; using var configurationRoot = new ConfigurationRoot(new List <IConfigurationProvider>() { firstProvider, secondProvider, }); var sut = new EnvironmentProvider(configurationRoot, null); var result = sut.ReadConfiguration(); result.Should().NotBeNull(); result.Should().HaveCount(3); // 3 because there is the default PID Provider of Net Core Admin var firstResult = result.First(); var lastResult = result.ElementAt(1); firstResult.Name.Should().Be("MemoryConfigurationProvider - 0"); lastResult.Name.Should().Be("MemoryConfigurationProvider - 1"); }
private BackTelemetryChannel Start(string assemblyName) { var customConfig = new MemoryConfigurationProvider(); customConfig.Set("server.urls", this.BaseHost); var configBuilder = new ConfigurationBuilder(); configBuilder.Add(customConfig); var config = configBuilder.Build(); var services = new ServiceCollection(); services.AddTransient <IApplicationEnvironment, ApplicationEnvironment>(); var serviceProvider = services.BuildServiceProvider(); var engine = CreateBuilder(config) .UseServer("Microsoft.AspNet.Server.WebListener") .UseStartup(assemblyName) .UseEnvironment("Production") .Build(); this.hostingEngine = engine.Start(); return((BackTelemetryChannel)engine.ApplicationServices.GetService <ITelemetryChannel>()); }
public static IConfigurationRoot GenerateConfiguration() { var configSource = new MemoryConfigurationSource { InitialData = new[] { new KeyValuePair <string, string>("ConfigurationStorageConnectionString", "UseDevelopmentStorage=true;"), new KeyValuePair <string, string>("ConfigNames", "SFA.DAS.FindApprenticeshipTraining.Web"), new KeyValuePair <string, string>("Environment", "DEV"), new KeyValuePair <string, string>("Version", "1.0"), new KeyValuePair <string, string>("FindApprenticeshipTrainingApi:Key", "test"), new KeyValuePair <string, string>("FindApprenticeshipTrainingApi:BaseUrl", "http://localhost:5003/"), new KeyValuePair <string, string>("FindApprenticeshipTrainingApi:PingUrl", "http://localhost:5003/"), new KeyValuePair <string, string>("FindApprenticeshipTrainingWeb:RedisConnectionString", ""), new KeyValuePair <string, string>("FindApprenticeshipTrainingWeb:DataProtectionKeysDatabase", ""), new KeyValuePair <string, string>("FindApprenticeshipTrainingWeb:ZendeskSectionId", "213452345"), new KeyValuePair <string, string>("FindApprenticeshipTrainingWeb:ZendeskSnippetKey", "e0730bdd"), new KeyValuePair <string, string>("FindApprenticeshipTrainingWeb:ZendeskCoBrowsingSnippetKey", "Qec2OgXsUy8") } }; var provider = new MemoryConfigurationProvider(configSource); return(new ConfigurationRoot(new List <IConfigurationProvider> { provider })); }
protected override void OnStart(string[] args) { try { var temp = new ConfigurationBuilder() .AddJsonFile("config.json") .AddJsonFile("hosting.json", true) .Build(); var configProvider = new MemoryConfigurationProvider(); configProvider.Add("server.urls", temp["WebServerAddress"]); configProvider.Add("webroot", temp.Get<string>("webroot", "wwwroot")); var config = new ConfigurationBuilder() .Add(configProvider) .Build(); var builder = new WebHostBuilder(config); builder.UseServer("Microsoft.AspNet.Server.Kestrel"); builder.UseStartup<Startup>(); var hostingEngine = builder.Build(); _application = hostingEngine.Start(); } catch (Exception ex) { Debug.WriteLine("error in OnStart: " + ex); throw; } }
public void ConfigurationToAppConfigurator() { "原本在 IConfiguration 存放的内容,可以通过 AppConfigurator 创建出来的配置读取到".Test(() => { // Arrange const string key = "LindexiIsDoubi"; const string value = "doubi"; var memoryConfigurationSource = new MemoryConfigurationSource() { InitialData = new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>(key, value) } }; IConfigurationProvider configurationProvider = new MemoryConfigurationProvider(memoryConfigurationSource); IConfiguration configuration = new ConfigurationRoot(new List <IConfigurationProvider>() { configurationProvider }); // Act var appConfigurator = configuration.ToAppConfigurator(); var configurationValue = appConfigurator.Default[key]; // Assert Assert.IsNotNull(configurationValue); Assert.AreEqual(value, configurationValue.ToString()); }); }
public void NewConfigurationProviderOverridesOldOneWhenKeyIsDuplicated() { // Arrange var dic1 = new Dictionary<string, string>() { {"Key1:Key2", "ValueInMem1"} }; var dic2 = new Dictionary<string, string>() { {"Key1:Key2", "ValueInMem2"} }; var memConfigSrc1 = new MemoryConfigurationProvider(dic1); var memConfigSrc2 = new MemoryConfigurationProvider(dic2); var configurationBuilder = new ConfigurationBuilder(); // Act configurationBuilder.Add(memConfigSrc1, load: false); configurationBuilder.Add(memConfigSrc2, load: false); var config = configurationBuilder.Build(); // Assert Assert.Equal("ValueInMem2", config["Key1:Key2"]); }
private void SetEnvironment() { IConfigurationRoot root = Configuration as IConfigurationRoot; if (root == null) { return; } MemoryConfigurationProvider provider = root.Providers.OfType <MemoryConfigurationProvider>().FirstOrDefault(); if (provider == null) { return; } string environment; if (!provider.TryGet(nameof(MessageBrokerEnvironment), out environment)) { throw new Exception("Environment not specified"); } environment = environment ?? "Test"; MessageBrokerEnvironment = environment.Parse <MessageBrokerEnvironment>(ignoreCase: true); MessageBrokerConfiguration = new ServerConfigurationManager().GetConfiguration(MessageBrokerEnvironment); }
private static IConfigurationRoot GenerateConfiguration(string authType) { var configSource = new MemoryConfigurationSource { InitialData = new[] { new KeyValuePair <string, string>("ConfigurationStorageConnectionString", "UseDevelopmentStorage=true;"), new KeyValuePair <string, string>("ConfigNames", "SFA.DAS.Reservations.Web,SFA.DAS.EmployerAccountAPI:AccountApi,SFA.DAS.ProviderRelationships.Api.ClientV2,SFA.DAS.Encoding"), new KeyValuePair <string, string>("Environment", "DEV"), new KeyValuePair <string, string>("Version", "1.0"), new KeyValuePair <string, string>("UseStub", "true"), new KeyValuePair <string, string>("AuthType", authType), new KeyValuePair <string, string>("ReservationsApi:url", "https://local.test.com"), new KeyValuePair <string, string>("ReservationsWeb:DashboardUrl", $"https://{TestDataValues.DashboardUrl}"), new KeyValuePair <string, string>("ReservationsWeb:EmployerDashboardUrl", $"https://{TestDataValues.EmployerDashboardUrl}"), new KeyValuePair <string, string>("ReservationsWeb:EmployerApprenticeUrl", $"https://{TestDataValues.EmployerApprenticeUrl}"), new KeyValuePair <string, string>("ReservationsWeb:FindApprenticeshipTrainingUrl", $"https://test"), new KeyValuePair <string, string>("ReservationsWeb:ApprenticeshipFundingRulesUrl", $"https://test") } }; var provider = new MemoryConfigurationProvider(configSource); return(new ConfigurationRoot(new List <IConfigurationProvider> { provider })); }
protected override void OnStart(string[] args) { try { var configProvider = new MemoryConfigurationProvider(); configProvider.Add("server.urls", "http://localhost:5000"); var config = new ConfigurationBuilder() .Add(configProvider) .Build(); var builder = new WebHostBuilder(config); builder.UseServer("Microsoft.AspNet.Server.Kestrel"); builder.UseServices(services => { services.AddMvc(opts => { // none }); }); builder.UseStartup(appBuilder => { appBuilder.Use(async (ctx, next) => { try { await next(); } catch (Exception ex) { Console.WriteLine(ex); } }); appBuilder.UseDefaultFiles(); appBuilder.UseStaticFiles(); appBuilder.UseMvc(); //if (env.IsDevelopment()) { appBuilder.UseBrowserLink(); appBuilder.UseDeveloperExceptionPage(); appBuilder.UseDatabaseErrorPage(); } }); var hostingEngine = builder.Build(); _application = hostingEngine.Start(); } catch (Exception ex) { Debug.WriteLine("error in OnStart: " + ex); throw; } }
protected override void OnStart(string[] args) { try { var configProvider = new MemoryConfigurationProvider(); configProvider.Add("server.urls", "http://localhost:5000"); var config = new ConfigurationBuilder() .Add(configProvider) .Build(); var builder = new WebHostBuilder(config); builder.UseServer("Microsoft.AspNet.Server.Kestrel"); builder.UseServices(services => { services.AddMvc(opts => { // none }); }); builder.UseStartup(appBuilder => { appBuilder.Use(async(ctx, next) => { try { await next(); } catch (Exception ex) { Console.WriteLine(ex); } }); appBuilder.UseDefaultFiles(); appBuilder.UseStaticFiles(); appBuilder.UseMvc(); //if (env.IsDevelopment()) { appBuilder.UseBrowserLink(); appBuilder.UseDeveloperExceptionPage(); appBuilder.UseDatabaseErrorPage(); } }); var hostingEngine = builder.Build(); _application = hostingEngine.Start(); } catch (Exception ex) { Debug.WriteLine("error in OnStart: " + ex); throw; } }
public IConfigurationProvider Build(IConfigurationBuilder builder) { var provider = new MemoryConfigurationProvider(this); provider.Add("MiniServer:HostName", "testminiserver.loxone.com"); provider.Add("MiniServer:Port", "7777"); provider.Add("MiniServer:UserName", "web"); provider.Add("MiniServer:Password", "web"); return(provider); }
private static IConfiguration SetUpDefaultAssembly(string assemblyName) { var source = new MemoryConfigurationProvider(); source.Add("defaultAssembly", assemblyName); var config = new ConfigurationBuilder(); config.Add(source); return(config.Build()); }
public void Main(string[] args) { // Work with in-memory configuration // Should print "First Red, then Blue" var builder = new ConfigurationBuilder(); var defaults = new MemoryConfigurationProvider { { "color", "Red" } }; builder.Add(defaults); var config = builder.Build(); Console.Write($"First {config["color"]}, then "); config["color"] = "Blue"; Console.WriteLine(config["color"]); // Add JSON configuration // Note ":" in keys to represent hierarchical structures var appPath = PlatformServices.Default.Application.ApplicationBasePath; builder.AddJsonFile(Path.Combine(appPath, "config.json")); Console.WriteLine($"Text is {config["text:color"]}, Circle is {config["circle:color"]}, and {config["color"]} is still there."); // Add command line // Try running "dnx myApp --color Green" builder.AddCommandLine(args); Console.WriteLine($"After adding command line it is {config["color"]}."); // Add environment variables // Try running the app after "SET color=Purple" builder.AddEnvironmentVariables(); Console.WriteLine($"After adding environment variables it is {config["color"]}."); // Now the "options pattern" var serviceCollection = new ServiceCollection(); serviceCollection.AddOptions(); serviceCollection.Configure <PaintOptions>(config.GetSection("paintOptions")); serviceCollection.AddTransient(typeof(ColorPrinter)); // Get options from service provider var provider = serviceCollection.BuildServiceProvider(); var opt = provider.GetService <IOptions <PaintOptions> >(); var oldForeground = Console.ForegroundColor; var oldBackground = Console.BackgroundColor; Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), opt.Value.Foreground); Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), opt.Value.Background); Console.WriteLine(opt.Value.Background); Console.ForegroundColor = oldForeground; Console.BackgroundColor = oldBackground; // Use options in depdency injection var painter = provider.GetService <ColorPrinter>(); }
private static void ChangeNonTypeSpecifiedConfig(IConfigurationRoot root, params KeyValuePair <string, string>[] additionalSettings) { MemoryConfigurationProvider provider = (MemoryConfigurationProvider)root.Providers.First(); provider.Set("foo:bar", "456"); foreach (var setting in additionalSettings) { provider.Set(setting.Key, setting.Value); } var _onReloadMethod = typeof(ConfigurationProvider).GetMethod("OnReload", BindingFlags.Instance | BindingFlags.NonPublic); _onReloadMethod.Invoke(provider, null); }
private void WhenIFindTheUrl() { var source = new MemoryConfigurationSource(); source.InitialData = _data; var provider = new MemoryConfigurationProvider(source); _config = new ConfigurationRoot(new List <IConfigurationProvider>() { provider }); _baseUrlFinder = new BaseUrlFinder(_config); _result = _baseUrlFinder.Find(); }
public void Startup() { IServiceCollection services = new ServiceCollection(); var context = new InventoryContext(); services.AddSingleton <IInventoryReadContext, InventoryContext>(p => context); services.AddSingleton <IInventoryWriteContext, InventoryContext>(p => context); Services = services.BuildServiceProvider(); var source = new MemoryConfigurationSource(); source.InitialData = new Dictionary <string, string>(); var b = new MemoryConfigurationProvider(source); }
public void CanGetConfigurationSection() { // Arrange var dic1 = new Dictionary <string, string>() { { "Data:DB1:Connection1", "MemVal1" }, { "Data:DB1:Connection2", "MemVal2" } }; var dic2 = new Dictionary <string, string>() { { "DataSource:DB2:Connection", "MemVal3" } }; var dic3 = new Dictionary <string, string>() { { "Data", "MemVal4" } }; var memConfigSrc1 = new MemoryConfigurationProvider(dic1); var memConfigSrc2 = new MemoryConfigurationProvider(dic2); var memConfigSrc3 = new MemoryConfigurationProvider(dic3); var builder = new ConfigurationBuilder(); builder.Add(memConfigSrc1, load: false); builder.Add(memConfigSrc2, load: false); builder.Add(memConfigSrc3, load: false); var config = builder.Build(); // Act var configFocus = config.GetSection("Data"); var memVal1 = configFocus["DB1:Connection1"]; var memVal2 = configFocus["DB1:Connection2"]; var memVal3 = configFocus["DB2:Connection"]; var memVal4 = configFocus["Source:DB2:Connection"]; var memVal5 = configFocus.Value; // Assert Assert.Equal("MemVal1", memVal1); Assert.Equal("MemVal2", memVal2); Assert.Equal("MemVal4", memVal5); Assert.Equal("MemVal1", configFocus["DB1:Connection1"]); Assert.Equal("MemVal2", configFocus["DB1:Connection2"]); Assert.Null(configFocus["DB2:Connection"]); Assert.Null(configFocus["Source:DB2:Connection"]); Assert.Equal("MemVal4", configFocus.Value); }
private static IConfigurationRoot GenerateConfiguration() { var configSource = new MemoryConfigurationSource { InitialData = new List <KeyValuePair <string, string> > { //new KeyValuePair<string, string>("EmployerDemandConfiguration:ConnectionString", "test"), } }; var provider = new MemoryConfigurationProvider(configSource); return(new ConfigurationRoot(new List <IConfigurationProvider> { provider })); }
public void TestBrokenAttribute() { var source = new MemoryConfigurationSource(); var provider = new MemoryConfigurationProvider(source); var ConfigurationRoot = new ConfigurationRoot(new[] { provider }); IServiceCollection servicesCollection = new ServiceCollection(); servicesCollection.AddSingleton(ConfigurationRoot); Assert.Throws <InvalidPropertyException>(() => { servicesCollection.AddConfigurationReader <IBrokenA>(); }); }
public void CanGetConfigurationSection() { // Arrange var dic1 = new Dictionary<string, string>() { {"Data:DB1:Connection1", "MemVal1"}, {"Data:DB1:Connection2", "MemVal2"} }; var dic2 = new Dictionary<string, string>() { {"DataSource:DB2:Connection", "MemVal3"} }; var dic3 = new Dictionary<string, string>() { {"Data", "MemVal4"} }; var memConfigSrc1 = new MemoryConfigurationProvider(dic1); var memConfigSrc2 = new MemoryConfigurationProvider(dic2); var memConfigSrc3 = new MemoryConfigurationProvider(dic3); var builder = new ConfigurationBuilder(); builder.Add(memConfigSrc1, load: false); builder.Add(memConfigSrc2, load: false); builder.Add(memConfigSrc3, load: false); var config = builder.Build(); // Act var configFocus = config.GetSection("Data"); var memVal1 = configFocus["DB1:Connection1"]; var memVal2 = configFocus["DB1:Connection2"]; var memVal3 = configFocus["DB2:Connection"]; var memVal4 = configFocus["Source:DB2:Connection"]; var memVal5 = configFocus.Value; // Assert Assert.Equal("MemVal1", memVal1); Assert.Equal("MemVal2", memVal2); Assert.Equal("MemVal4", memVal5); Assert.Equal("MemVal1", configFocus["DB1:Connection1"]); Assert.Equal("MemVal2", configFocus["DB1:Connection2"]); Assert.Null(configFocus["DB2:Connection"]); Assert.Null(configFocus["Source:DB2:Connection"]); Assert.Equal("MemVal4", configFocus.Value); }
private static IConfigurationRoot GenerateConfiguration() { var configSource = new MemoryConfigurationSource { InitialData = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("EmployerDemandApi:BaseUrl", "http://localhost:1"), new KeyValuePair <string, string>("EmployerDemandApi:Key", "test") } }; var provider = new MemoryConfigurationProvider(configSource); return(new ConfigurationRoot(new List <IConfigurationProvider> { provider })); }
private BackTelemetryChannel Start(string assemblyName) { var customConfig = new MemoryConfigurationProvider(); customConfig.Set("server.urls", this.BaseHost); var configBuilder = new ConfigurationBuilder(); configBuilder.Add(customConfig); var config = configBuilder.Build(); var engine = CreateBuilder(config) .UseServer("Microsoft.AspNet.Server.WebListener") .UseStartup(assemblyName) .UseEnvironment("Production") .Build(); this.hostingEngine = engine.Start(); return (BackTelemetryChannel)engine.ApplicationServices.GetService<ITelemetryChannel>(); }
public void LoadAndCombineKeyValuePairsFromDifferentConfigurationProviders() { // Arrange var dic1 = new Dictionary <string, string>() { { "Mem1:KeyInMem1", "ValueInMem1" } }; var dic2 = new Dictionary <string, string>() { { "Mem2:KeyInMem2", "ValueInMem2" } }; var dic3 = new Dictionary <string, string>() { { "Mem3:KeyInMem3", "ValueInMem3" } }; var memConfigSrc1 = new MemoryConfigurationProvider(dic1); var memConfigSrc2 = new MemoryConfigurationProvider(dic2); var memConfigSrc3 = new MemoryConfigurationProvider(dic3); var builder = new ConfigurationBuilder(); // Act builder.Add(memConfigSrc1, load: false); builder.Add(memConfigSrc2, load: false); builder.Add(memConfigSrc3, load: false); var config = builder.Build(); var memVal1 = config["mem1:keyinmem1"]; var memVal2 = config["Mem2:KeyInMem2"]; var memVal3 = config["MEM3:KEYINMEM3"]; // Assert Assert.Contains(memConfigSrc1, builder.Providers); Assert.Contains(memConfigSrc2, builder.Providers); Assert.Contains(memConfigSrc3, builder.Providers); Assert.Equal("ValueInMem1", memVal1); Assert.Equal("ValueInMem2", memVal2); Assert.Equal("ValueInMem3", memVal3); Assert.Equal("ValueInMem1", config["mem1:keyinmem1"]); Assert.Equal("ValueInMem2", config["Mem2:KeyInMem2"]); Assert.Equal("ValueInMem3", config["MEM3:KEYINMEM3"]); Assert.Null(config["NotExist"]); }
private static IConfigurationRoot GenerateConfiguration() { var configSource = new MemoryConfigurationSource { InitialData = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("AccountsInnerApi:url", "http://localhost:1"), new KeyValuePair <string, string>("ApimDeveloperApiConfiguration:url", "http://localhost:2") } }; var provider = new MemoryConfigurationProvider(configSource); return(new ConfigurationRoot(new List <IConfigurationProvider> { provider })); }
public void LoadAndCombineKeyValuePairsFromDifferentConfigurationProviders() { // Arrange var dic1 = new Dictionary<string, string>() { {"Mem1:KeyInMem1", "ValueInMem1"} }; var dic2 = new Dictionary<string, string>() { {"Mem2:KeyInMem2", "ValueInMem2"} }; var dic3 = new Dictionary<string, string>() { {"Mem3:KeyInMem3", "ValueInMem3"} }; var memConfigSrc1 = new MemoryConfigurationProvider(dic1); var memConfigSrc2 = new MemoryConfigurationProvider(dic2); var memConfigSrc3 = new MemoryConfigurationProvider(dic3); var configurationBuilder = new ConfigurationBuilder(); // Act configurationBuilder.Add(memConfigSrc1, load: false); configurationBuilder.Add(memConfigSrc2, load: false); configurationBuilder.Add(memConfigSrc3, load: false); var config = configurationBuilder.Build(); var memVal1 = config["mem1:keyinmem1"]; var memVal2 = config["Mem2:KeyInMem2"]; var memVal3 = config["MEM3:KEYINMEM3"]; // Assert Assert.Contains(memConfigSrc1, configurationBuilder.Providers); Assert.Contains(memConfigSrc2, configurationBuilder.Providers); Assert.Contains(memConfigSrc3, configurationBuilder.Providers); Assert.Equal("ValueInMem1", memVal1); Assert.Equal("ValueInMem2", memVal2); Assert.Equal("ValueInMem3", memVal3); Assert.Equal("ValueInMem1", config["mem1:keyinmem1"]); Assert.Equal("ValueInMem2", config["Mem2:KeyInMem2"]); Assert.Equal("ValueInMem3", config["MEM3:KEYINMEM3"]); Assert.Null(config["NotExist"]); }
public void Main(string[] args) { var builder = new ConfigurationBuilder(); Console.WriteLine("Initial Config Providers: " + builder.Providers.Count()); var defaultSettings = new MemoryConfigurationProvider(); defaultSettings.Set("username", "Guest"); builder.Add(defaultSettings); Console.WriteLine("Added Memory Provider. Providers: " + builder.Providers.Count()); builder.AddCommandLine(args); Console.WriteLine("Added Command Line Provider. Providers: " + builder.Providers.Count()); var config = builder.Build(); string username = config["username"]; Console.WriteLine($"Hello, {username}!"); }
public void CanGetConfigurationChildren() { // Arrange var dic1 = new Dictionary<string, string>() { {"Data:DB1:Connection1", "MemVal1"}, {"Data:DB1:Connection2", "MemVal2"} }; var dic2 = new Dictionary<string, string>() { {"Data:DB2Connection", "MemVal3"} }; var dic3 = new Dictionary<string, string>() { {"DataSource:DB3:Connection", "MemVal4"} }; var memConfigSrc1 = new MemoryConfigurationProvider(dic1); var memConfigSrc2 = new MemoryConfigurationProvider(dic2); var memConfigSrc3 = new MemoryConfigurationProvider(dic3); var builder = new ConfigurationBuilder(); builder.Add(memConfigSrc1, load: false); builder.Add(memConfigSrc2, load: false); builder.Add(memConfigSrc3, load: false); var config = builder.Build(); // Act var configSections = config.GetSection("Data").GetChildren().ToList(); // Assert Assert.Equal(2, configSections.Count()); Assert.Equal("MemVal1", configSections.FirstOrDefault(c => c.Key == "DB1")["Connection1"]); Assert.Equal("MemVal2", configSections.FirstOrDefault(c => c.Key == "DB1")["Connection2"]); Assert.Equal("MemVal3", configSections.FirstOrDefault(c => c.Key == "DB2Connection").Value); Assert.False(configSections.Exists(c => c.Key == "DB3")); Assert.False(configSections.Exists(c => c.Key == "DB3")); }
private static IConfiguration SetUpDefaultAssembly(string assemblyName) { var source = new MemoryConfigurationProvider(); source.Add("defaultAssembly", assemblyName); var config = new ConfigurationBuilder(); config.Add(source); return config.Build(); }
public void SettingValueUpdatesAllConfigurationProviders() { // Arrange var dict = new Dictionary<string, string>() { {"Key1", "Value1"}, {"Key2", "Value2"} }; var memConfigSrc1 = new MemoryConfigurationProvider(dict); var memConfigSrc2 = new MemoryConfigurationProvider(dict); var memConfigSrc3 = new MemoryConfigurationProvider(dict); var configurationBuilder = new ConfigurationBuilder(); configurationBuilder.Add(memConfigSrc1, load: false); configurationBuilder.Add(memConfigSrc2, load: false); configurationBuilder.Add(memConfigSrc3, load: false); var config = configurationBuilder.Build(); // Act config["Key1"] = "NewValue1"; config["Key2"] = "NewValue2"; // Assert Assert.Equal("NewValue1", config["Key1"]); Assert.Equal("NewValue1", memConfigSrc1.Get("Key1")); Assert.Equal("NewValue1", memConfigSrc2.Get("Key1")); Assert.Equal("NewValue1", memConfigSrc3.Get("Key1")); Assert.Equal("NewValue2", config["Key2"]); Assert.Equal("NewValue2", memConfigSrc1.Get("Key2")); Assert.Equal("NewValue2", memConfigSrc2.Get("Key2")); Assert.Equal("NewValue2", memConfigSrc3.Get("Key2")); }
public void SourcesReturnsAddedConfigurationProviders() { // Arrange var dict = new Dictionary<string, string>() { {"Mem:KeyInMem", "MemVal"} }; var memConfigSrc1 = new MemoryConfigurationProvider(dict); var memConfigSrc2 = new MemoryConfigurationProvider(dict); var memConfigSrc3 = new MemoryConfigurationProvider(dict); var srcSet = new HashSet<IConfigurationProvider>() { memConfigSrc1, memConfigSrc2, memConfigSrc3 }; var configurationBuilder = new ConfigurationBuilder(); // Act configurationBuilder.Add(memConfigSrc1, load: false); configurationBuilder.Add(memConfigSrc2, load: false); configurationBuilder.Add(memConfigSrc3, load: false); var config = configurationBuilder.Build(); // Assert Assert.Equal(new[] { memConfigSrc1, memConfigSrc2, memConfigSrc3 }, configurationBuilder.Providers); }