From db4b0af98b659c88efdd8b1285d35d2c3790ab4c Mon Sep 17 00:00:00 2001 From: John Luo Date: Tue, 7 Jun 2016 11:50:17 -0700 Subject: [PATCH] Read environment variables by default #784 --- .../WebHostBuilder.cs | 29 ++++++++++++++----- src/Microsoft.AspNetCore.Hosting/project.json | 1 + 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs b/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs index a72b63ee9a..e4b2e91963 100644 --- a/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs +++ b/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs @@ -27,7 +27,7 @@ namespace Microsoft.AspNetCore.Hosting private readonly List> _configureServicesDelegates; private readonly List> _configureLoggingDelegates; - private IConfiguration _config = new ConfigurationBuilder().AddInMemoryCollection().Build(); + private IConfiguration _config; private ILoggerFactory _loggerFactory; private WebHostOptions _options; @@ -40,14 +40,22 @@ namespace Microsoft.AspNetCore.Hosting _configureServicesDelegates = new List>(); _configureLoggingDelegates = new List>(); - // This may end up storing null, but that's indistinguishable from not adding it. - UseSetting(WebHostDefaults.EnvironmentKey, Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") - // Legacy keys, never remove these. - ?? Environment.GetEnvironmentVariable("Hosting:Environment") - ?? Environment.GetEnvironmentVariable("ASPNET_ENV")); + _config = new ConfigurationBuilder() + .AddEnvironmentVariables(prefix: "ASPNETCORE_") + .Build(); - // Add the default server.urls key - UseSetting(WebHostDefaults.ServerUrlsKey, Environment.GetEnvironmentVariable("ASPNETCORE_URLS")); + if (string.IsNullOrEmpty(GetSetting(WebHostDefaults.EnvironmentKey))) + { + // Try adding legacy environment keys, never remove these. + UseSetting(WebHostDefaults.EnvironmentKey, Environment.GetEnvironmentVariable("Hosting:Environment") + ?? Environment.GetEnvironmentVariable("ASPNET_ENV")); + } + + if (string.IsNullOrEmpty(GetSetting(WebHostDefaults.ServerUrlsKey))) + { + // Try adding legacy url key, never remove this. + UseSetting(WebHostDefaults.ServerUrlsKey, Environment.GetEnvironmentVariable("ASPNETCORE_SERVER.URLS")); + } } /// @@ -137,6 +145,11 @@ namespace Microsoft.AspNetCore.Hosting Console.WriteLine("The environment variable 'ASPNET_ENV' is obsolete and has been replaced with 'ASPNETCORE_ENVIRONMENT'"); } + if (Environment.GetEnvironmentVariable("ASPNETCORE_SERVER.URLS") != null) + { + Console.WriteLine("The environment variable 'ASPNETCORE_SERVER.URLS' is obsolete and has been replaced with 'ASPNETCORE_URLS'"); + } + var hostingServices = BuildHostingServices(); var hostingContainer = hostingServices.BuildServiceProvider(); diff --git a/src/Microsoft.AspNetCore.Hosting/project.json b/src/Microsoft.AspNetCore.Hosting/project.json index 12a7336b40..859d106f50 100644 --- a/src/Microsoft.AspNetCore.Hosting/project.json +++ b/src/Microsoft.AspNetCore.Hosting/project.json @@ -27,6 +27,7 @@ "Microsoft.Extensions.FileProviders.Physical": "1.0.0-*", "Microsoft.Extensions.Options": "1.0.0-*", "Microsoft.Extensions.Configuration": "1.0.0-*", + "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-*", "Microsoft.Extensions.DependencyInjection": "1.0.0-*", "Microsoft.Extensions.Logging": "1.0.0-*", "Microsoft.Extensions.PlatformAbstractions": "1.0.0-*",