Fix bug with environment name beeng overwritten
After being set using UseEnvironment
This commit is contained in:
parent
b2edb0d484
commit
ecb7e697d1
|
|
@ -27,6 +27,7 @@ namespace Microsoft.AspNet.Hosting
|
|||
private readonly WebHostOptions _options;
|
||||
|
||||
private Action<IServiceCollection> _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<IStartupLoader>();
|
||||
|
||||
_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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -124,6 +124,23 @@ namespace Microsoft.AspNet.Hosting
|
|||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UseEnvironmentIsNotOverriden()
|
||||
{
|
||||
var vals = new Dictionary<string, string>
|
||||
{
|
||||
{ "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<IHostingEnvironment>().EnvironmentName);
|
||||
}
|
||||
|
||||
private WebHostBuilder CreateWebHostBuilder()
|
||||
{
|
||||
var vals = new Dictionary<string, string>
|
||||
|
|
|
|||
Loading…
Reference in New Issue