From ecb7e697d15033918147e286dc198865261beefc Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Tue, 10 Nov 2015 16:48:16 -0800 Subject: [PATCH] Fix bug with environment name beeng overwritten After being set using UseEnvironment --- src/Microsoft.AspNet.Hosting/WebHostBuilder.cs | 7 ++++++- .../WebHostBuilderTests.cs | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNet.Hosting/WebHostBuilder.cs b/src/Microsoft.AspNet.Hosting/WebHostBuilder.cs index a26796412f..f60c32fb32 100644 --- a/src/Microsoft.AspNet.Hosting/WebHostBuilder.cs +++ b/src/Microsoft.AspNet.Hosting/WebHostBuilder.cs @@ -27,6 +27,7 @@ namespace Microsoft.AspNet.Hosting private readonly WebHostOptions _options; private Action _configureServices; + private string _environmentName; // Only one of these should be set private StartupMethods _startup; @@ -127,6 +128,10 @@ namespace Microsoft.AspNet.Hosting var startupLoader = hostingContainer.GetRequiredService(); _hostingEnvironment.Initialize(appEnvironment.ApplicationBasePath, _options); + if (!string.IsNullOrEmpty(_environmentName)) + { + _hostingEnvironment.EnvironmentName = _environmentName; + } var engine = new HostingEngine(hostingServices, startupLoader, _options, _config, _captureStartupErrors); // Only one of these should be set, but they are used in priority @@ -155,7 +160,7 @@ namespace Microsoft.AspNet.Hosting throw new ArgumentNullException(nameof(environment)); } - _hostingEnvironment.EnvironmentName = environment; + _environmentName = environment; return this; } diff --git a/test/Microsoft.AspNet.Hosting.Tests/WebHostBuilderTests.cs b/test/Microsoft.AspNet.Hosting.Tests/WebHostBuilderTests.cs index db801c2f4d..7b3bdf7f82 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/WebHostBuilderTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/WebHostBuilderTests.cs @@ -124,6 +124,23 @@ namespace Microsoft.AspNet.Hosting } } + [Fact] + public void UseEnvironmentIsNotOverriden() + { + var vals = new Dictionary + { + { "ENV", "Dev" }, + }; + var builder = new ConfigurationBuilder() + .AddInMemoryCollection(vals); + var config = builder.Build(); + + var expected = "MY_TEST_ENVIRONMENT"; + var webHost = new WebHostBuilder(config, captureStartupErrors: true).UseEnvironment(expected).Build(); + + Assert.Equal(expected, webHost.ApplicationServices.GetService().EnvironmentName); + } + private WebHostBuilder CreateWebHostBuilder() { var vals = new Dictionary