/// <summary> /// Create host builder using the LayeredSettingsHost /// </summary> /// <param name="args">command line arguments</param> /// <returns>host builder</returns> public static IHostBuilder CreateHostBuilder(string[] args) => LayeredSettingsHost .CreateHostBuilder(args, SampleEnvironments.HostEnvironmentList) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup <Startup>(); });
public void CreateHostBuilder_ObserveEnvironmentOverrideFromAppSettingsJson() { var reloadFlagConfig = new Dictionary <string, string>() { { "hostbuilder:reloadConfigOnChange", "false" } }; var guid = Guid.NewGuid().ToString(); var devEnvName = $"Development{guid}"; var rootFileName = "appsettings.json"; var devFileName = $"appsettings.{devEnvName}.json"; var tempPath = Path.GetTempPath(); var appSettingsPath = Path.Combine(tempPath, rootFileName); var appSettingsDEVPath = Path.Combine(tempPath, devFileName); SaveConfig(appSettingsPath, rootFileName); var builder = LayeredSettingsHost.CreateHostBuilder(null, null).UseContentRoot(tempPath); var config = builder.Build().Services.GetRequiredService <IConfiguration>(); Assert.Equal(rootFileName, config["FileName"]); // environment specified, observe it based on how it's configured SaveConfig(appSettingsDEVPath, devEnvName); builder = LayeredSettingsHost.CreateHostBuilder(null, new[] { new HostEnvironment(devEnvName) }) .UseContentRoot(tempPath) .UseEnvironment(devEnvName); config = builder.Build().Services.GetRequiredService <IConfiguration>(); Assert.Equal(devEnvName, config["FileName"]); // no environments are specified, default to asp.net core behavior builder = LayeredSettingsHost.CreateHostBuilder(null, null) .UseContentRoot(tempPath) .UseEnvironment(devEnvName); config = builder.Build().Services.GetRequiredService <IConfiguration>(); Assert.Equal(devEnvName, config["FileName"]); }
public void CreateHostBuilder_ObserveEnvironmentLayering() { var reloadFlagConfig = new Dictionary <string, string>() { { "hostbuilder:reloadConfigOnChange", "false" } }; var guid = Guid.NewGuid().ToString(); var ppeEnvName = $"PPE{guid}"; var devEnvName = $"Development{guid}"; var ppeFileName = $"appsettings.{ppeEnvName}.json"; var devFileName = $"appsettings.{devEnvName}.json"; var tempPath = Path.GetTempPath(); var appSettingsPPEPath = Path.Combine(tempPath, ppeFileName); var appSettingsDEVPath = Path.Combine(tempPath, devFileName); var ppeEnv = new HostEnvironment(ppeEnvName); var devEnv = new HostEnvironment(devEnvName, ppeEnv); SaveConfig(appSettingsPPEPath, ppeEnvName); SaveConfig(appSettingsDEVPath, devEnvName); // PPE setting is used var builder = LayeredSettingsHost.CreateHostBuilder(null, new[] { ppeEnv, devEnv }) .UseContentRoot(tempPath) .UseEnvironment(ppeEnvName); var config = builder.Build().Services.GetRequiredService <IConfiguration>(); Assert.Equal(ppeEnvName, config["FileName"]); // Dev setting is used and it overrides PPE builder = LayeredSettingsHost.CreateHostBuilder(null, new[] { ppeEnv, devEnv }) .UseContentRoot(tempPath) .UseEnvironment(devEnvName); config = builder.Build().Services.GetRequiredService <IConfiguration>(); Assert.Equal(devEnvName, config["FileName"]); }
public void CreateHostBuilder_DoesntThrowOnNullEnvironments() { var builder = LayeredSettingsHost.CreateHostBuilder(null, null); builder.Build(); }