diff --git a/src/libraries/Microsoft.Extensions.Hosting/src/Host.cs b/src/libraries/Microsoft.Extensions.Hosting/src/Host.cs index f1d095ab0c7e23..69e59b9d5615e8 100644 --- a/src/libraries/Microsoft.Extensions.Hosting/src/Host.cs +++ b/src/libraries/Microsoft.Extensions.Hosting/src/Host.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Collections.Generic; using System.IO; using System.Reflection; using System.Runtime.InteropServices; @@ -57,9 +58,12 @@ public static IHostBuilder CreateDefaultBuilder(string[] args) { var builder = new HostBuilder(); - builder.UseContentRoot(Directory.GetCurrentDirectory()); builder.ConfigureHostConfiguration(config => { + config.AddInMemoryCollection(new[] + { + new KeyValuePair (HostDefaults.ContentRootKey, Directory.GetCurrentDirectory()) + }); config.AddEnvironmentVariables(prefix: "DOTNET_"); if (args != null) { diff --git a/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs b/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs index a36a69858e4c34..98b3525df871f0 100644 --- a/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs +++ b/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs @@ -147,11 +147,26 @@ private void BuildHostConfiguration() private void CreateHostingEnvironment() { + // Explicit settings override config. + string environmentName = _hostConfiguration[HostDefaults.EnvironmentKey] ?? Environments.Production; + if (Properties.TryGetValue(HostDefaults.EnvironmentKey, out object envObj) + && (envObj is string env)) + { + environmentName = env; + } + + string contentRoot = _hostConfiguration[HostDefaults.ContentRootKey]; + if (Properties.TryGetValue(HostDefaults.ContentRootKey, out object contentObj) + && (contentObj is string content)) + { + contentRoot = content; + } + _hostingEnvironment = new HostingEnvironment() { ApplicationName = _hostConfiguration[HostDefaults.ApplicationKey], - EnvironmentName = _hostConfiguration[HostDefaults.EnvironmentKey] ?? Environments.Production, - ContentRootPath = ResolveContentRootPath(_hostConfiguration[HostDefaults.ContentRootKey], AppContext.BaseDirectory), + EnvironmentName = environmentName, + ContentRootPath = ResolveContentRootPath(contentRoot, AppContext.BaseDirectory), }; if (string.IsNullOrEmpty(_hostingEnvironment.ApplicationName)) diff --git a/src/libraries/Microsoft.Extensions.Hosting/src/HostingHostBuilderExtensions.cs b/src/libraries/Microsoft.Extensions.Hosting/src/HostingHostBuilderExtensions.cs index ca7e84abff8a07..19a1d9cbbbae02 100644 --- a/src/libraries/Microsoft.Extensions.Hosting/src/HostingHostBuilderExtensions.cs +++ b/src/libraries/Microsoft.Extensions.Hosting/src/HostingHostBuilderExtensions.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; -using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Configuration; @@ -22,14 +21,8 @@ public static class HostingHostBuilderExtensions /// The . public static IHostBuilder UseEnvironment(this IHostBuilder hostBuilder, string environment) { - return hostBuilder.ConfigureHostConfiguration(configBuilder => - { - configBuilder.AddInMemoryCollection(new[] - { - new KeyValuePair(HostDefaults.EnvironmentKey, - environment ?? throw new ArgumentNullException(nameof(environment))) - }); - }); + hostBuilder.Properties[HostDefaults.EnvironmentKey] = environment; + return hostBuilder; } /// @@ -40,14 +33,8 @@ public static IHostBuilder UseEnvironment(this IHostBuilder hostBuilder, string /// The . public static IHostBuilder UseContentRoot(this IHostBuilder hostBuilder, string contentRoot) { - return hostBuilder.ConfigureHostConfiguration(configBuilder => - { - configBuilder.AddInMemoryCollection(new[] - { - new KeyValuePair(HostDefaults.ContentRootKey, - contentRoot ?? throw new ArgumentNullException(nameof(contentRoot))) - }); - }); + hostBuilder.Properties[HostDefaults.ContentRootKey] = contentRoot; + return hostBuilder; } /// diff --git a/src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/HostBuilderTests.cs b/src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/HostBuilderTests.cs index 5c1a3b67d1287c..f22810784000bd 100644 --- a/src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/HostBuilderTests.cs +++ b/src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/HostBuilderTests.cs @@ -202,22 +202,38 @@ public void ConfigBasedSettingsConfigBasedOverride() } [Fact] - public void UseEnvironmentIsNotOverriden() + public void UseEnvironmentOverridesConfig() { var vals = new Dictionary { - { "ENV", "Dev" }, + { HostDefaults.EnvironmentKey, "Dev" }, + }; + + var expected = "MY_TEST_ENVIRONMENT"; + + using (var host = new HostBuilder() + .ConfigureHostConfiguration(configBuilder => configBuilder.AddInMemoryCollection(vals)) + .UseEnvironment(expected) + .Build()) + { + Assert.Equal(expected, host.Services.GetService().EnvironmentName); + } + } + + [Fact] + public void UseEnvironmentIsNotOverridenByConfig() + { + var vals = new Dictionary + { + { HostDefaults.EnvironmentKey, "Dev" }, }; - var builder = new ConfigurationBuilder() - .AddInMemoryCollection(vals); - var config = builder.Build(); var expected = "MY_TEST_ENVIRONMENT"; using (var host = new HostBuilder() - .ConfigureHostConfiguration(configBuilder => configBuilder.AddConfiguration(config)) .UseEnvironment(expected) + .ConfigureHostConfiguration(configBuilder => configBuilder.AddInMemoryCollection(vals)) .Build()) { Assert.Equal(expected, host.Services.GetService().EnvironmentName); @@ -232,19 +248,44 @@ public void BuildAndDispose() } [Fact] - public void UseBasePathConfiguresBasePath() + public void UseContentRootConfiguresContentRoot() + { + using (var host = new HostBuilder() + .UseContentRoot("/") + .Build()) + { + Assert.Equal("/", host.Services.GetService().ContentRootPath); + } + } + + [Fact] + public void UseContentRootOverridesConfig() { var vals = new Dictionary { - { "ENV", "Dev" }, + { HostDefaults.ContentRootKey, "Foo" }, + }; + + using (var host = new HostBuilder() + .ConfigureHostConfiguration(configBuilder => configBuilder.AddInMemoryCollection(vals)) + .UseContentRoot("/") + .Build()) + { + Assert.Equal("/", host.Services.GetService().ContentRootPath); + } + } + + [Fact] + public void UseContentRootNotOverridenByConfig() + { + var vals = new Dictionary + { + { HostDefaults.ContentRootKey, "Foo" }, }; - var builder = new ConfigurationBuilder() - .AddInMemoryCollection(vals); - var config = builder.Build(); using (var host = new HostBuilder() - .ConfigureHostConfiguration(configBuilder => configBuilder.AddConfiguration(config)) .UseContentRoot("/") + .ConfigureHostConfiguration(configBuilder => configBuilder.AddInMemoryCollection(vals)) .Build()) { Assert.Equal("/", host.Services.GetService().ContentRootPath);