Read environment variables by default #784

This commit is contained in:
John Luo 2016-06-07 11:50:17 -07:00
parent e41b455141
commit db4b0af98b
2 changed files with 22 additions and 8 deletions

View File

@ -27,7 +27,7 @@ namespace Microsoft.AspNetCore.Hosting
private readonly List<Action<IServiceCollection>> _configureServicesDelegates; private readonly List<Action<IServiceCollection>> _configureServicesDelegates;
private readonly List<Action<ILoggerFactory>> _configureLoggingDelegates; private readonly List<Action<ILoggerFactory>> _configureLoggingDelegates;
private IConfiguration _config = new ConfigurationBuilder().AddInMemoryCollection().Build(); private IConfiguration _config;
private ILoggerFactory _loggerFactory; private ILoggerFactory _loggerFactory;
private WebHostOptions _options; private WebHostOptions _options;
@ -40,14 +40,22 @@ namespace Microsoft.AspNetCore.Hosting
_configureServicesDelegates = new List<Action<IServiceCollection>>(); _configureServicesDelegates = new List<Action<IServiceCollection>>();
_configureLoggingDelegates = new List<Action<ILoggerFactory>>(); _configureLoggingDelegates = new List<Action<ILoggerFactory>>();
// This may end up storing null, but that's indistinguishable from not adding it. _config = new ConfigurationBuilder()
UseSetting(WebHostDefaults.EnvironmentKey, Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") .AddEnvironmentVariables(prefix: "ASPNETCORE_")
// Legacy keys, never remove these. .Build();
?? Environment.GetEnvironmentVariable("Hosting:Environment")
?? Environment.GetEnvironmentVariable("ASPNET_ENV"));
// Add the default server.urls key if (string.IsNullOrEmpty(GetSetting(WebHostDefaults.EnvironmentKey)))
UseSetting(WebHostDefaults.ServerUrlsKey, Environment.GetEnvironmentVariable("ASPNETCORE_URLS")); {
// 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"));
}
} }
/// <summary> /// <summary>
@ -137,6 +145,11 @@ namespace Microsoft.AspNetCore.Hosting
Console.WriteLine("The environment variable 'ASPNET_ENV' is obsolete and has been replaced with 'ASPNETCORE_ENVIRONMENT'"); 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 hostingServices = BuildHostingServices();
var hostingContainer = hostingServices.BuildServiceProvider(); var hostingContainer = hostingServices.BuildServiceProvider();

View File

@ -27,6 +27,7 @@
"Microsoft.Extensions.FileProviders.Physical": "1.0.0-*", "Microsoft.Extensions.FileProviders.Physical": "1.0.0-*",
"Microsoft.Extensions.Options": "1.0.0-*", "Microsoft.Extensions.Options": "1.0.0-*",
"Microsoft.Extensions.Configuration": "1.0.0-*", "Microsoft.Extensions.Configuration": "1.0.0-*",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-*",
"Microsoft.Extensions.DependencyInjection": "1.0.0-*", "Microsoft.Extensions.DependencyInjection": "1.0.0-*",
"Microsoft.Extensions.Logging": "1.0.0-*", "Microsoft.Extensions.Logging": "1.0.0-*",
"Microsoft.Extensions.PlatformAbstractions": "1.0.0-*", "Microsoft.Extensions.PlatformAbstractions": "1.0.0-*",