diff --git a/src/Microsoft.AspNetCore.Hosting.Abstractions/WebHostDefaults.cs b/src/Microsoft.AspNetCore.Hosting.Abstractions/WebHostDefaults.cs index 208a8311e0..ac9003bab6 100644 --- a/src/Microsoft.AspNetCore.Hosting.Abstractions/WebHostDefaults.cs +++ b/src/Microsoft.AspNetCore.Hosting.Abstractions/WebHostDefaults.cs @@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Hosting public static readonly string EnvironmentKey = "environment"; public static readonly string WebRootKey = "webroot"; public static readonly string CaptureStartupErrorsKey = "captureStartupErrors"; - public static readonly string ServerUrlsKey = "server.urls"; + public static readonly string ServerUrlsKey = "urls"; public static readonly string ContentRootKey = "contentRoot"; } } diff --git a/src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs b/src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs index 5d49f97aaf..c2a591b5b8 100644 --- a/src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs +++ b/src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs @@ -20,6 +20,8 @@ namespace Microsoft.AspNetCore.Hosting.Internal { public class WebHost : IWebHost { + private static readonly string DeprecatedServerUrlsKey = "server.urls"; + private readonly IServiceCollection _applicationServiceCollection; private IStartup _startup; @@ -188,7 +190,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal var addresses = Server.Features?.Get()?.Addresses; if (addresses != null && !addresses.IsReadOnly && addresses.Count == 0) { - var urls = _config[WebHostDefaults.ServerUrlsKey]; + var urls = _config[WebHostDefaults.ServerUrlsKey] ?? _config[DeprecatedServerUrlsKey]; if (!string.IsNullOrEmpty(urls)) { foreach (var value in urls.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) diff --git a/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs b/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs index df2c8f5e41..a72b63ee9a 100644 --- a/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs +++ b/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs @@ -9,7 +9,6 @@ using System.Reflection; using System.Runtime.ExceptionServices; using Microsoft.AspNetCore.Hosting.Builder; using Microsoft.AspNetCore.Hosting.Internal; -using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -47,14 +46,8 @@ namespace Microsoft.AspNetCore.Hosting ?? Environment.GetEnvironmentVariable("Hosting:Environment") ?? Environment.GetEnvironmentVariable("ASPNET_ENV")); - if (Environment.GetEnvironmentVariable("Hosting:Environment") != null) - { - Console.WriteLine("The environment variable 'Hosting:Environment' is obsolete and has been replaced with 'ASPNETCORE_ENVIRONMENT'"); - } - if (Environment.GetEnvironmentVariable("ASPNET_ENV") != null) - { - Console.WriteLine("The environment variable 'ASPNET_ENV' is obsolete and has been replaced with 'ASPNETCORE_ENVIRONMENT'"); - } + // Add the default server.urls key + UseSetting(WebHostDefaults.ServerUrlsKey, Environment.GetEnvironmentVariable("ASPNETCORE_URLS")); } /// @@ -133,6 +126,17 @@ namespace Microsoft.AspNetCore.Hosting /// public IWebHost Build() { + // Warn about deprecated environment variables + if (Environment.GetEnvironmentVariable("Hosting:Environment") != null) + { + Console.WriteLine("The environment variable 'Hosting:Environment' is obsolete and has been replaced with 'ASPNETCORE_ENVIRONMENT'"); + } + + if (Environment.GetEnvironmentVariable("ASPNET_ENV") != null) + { + Console.WriteLine("The environment variable 'ASPNET_ENV' is obsolete and has been replaced with 'ASPNETCORE_ENVIRONMENT'"); + } + var hostingServices = BuildHostingServices(); var hostingContainer = hostingServices.BuildServiceProvider(); @@ -192,7 +196,7 @@ namespace Microsoft.AspNetCore.Hosting try { var startupType = StartupLoader.FindStartupType(_options.StartupAssembly, _hostingEnvironment.EnvironmentName); - + if (typeof(IStartup).GetTypeInfo().IsAssignableFrom(startupType.GetTypeInfo())) { services.AddSingleton(typeof(IStartup), startupType); diff --git a/test/Microsoft.AspNetCore.Hosting.Tests/WebHostTests.cs b/test/Microsoft.AspNetCore.Hosting.Tests/WebHostTests.cs index ab8f3255c2..472a027392 100644 --- a/test/Microsoft.AspNetCore.Hosting.Tests/WebHostTests.cs +++ b/test/Microsoft.AspNetCore.Hosting.Tests/WebHostTests.cs @@ -75,6 +75,52 @@ namespace Microsoft.AspNetCore.Hosting Assert.Equal("http://localhost:5000", host.ServerFeatures.Get().Addresses.First()); } + [Fact] + public void UsesLegacyConfigurationForAddresses() + { + var data = new Dictionary + { + { "server.urls", "http://localhost:5002" } + }; + + var config = new ConfigurationBuilder().AddInMemoryCollection(data).Build(); + + var host = CreateBuilder(config).UseServer(this).Build(); + host.Start(); + Assert.Equal("http://localhost:5002", host.ServerFeatures.Get().Addresses.First()); + } + + [Fact] + public void UsesConfigurationForAddresses() + { + var data = new Dictionary + { + { "urls", "http://localhost:5003" } + }; + + var config = new ConfigurationBuilder().AddInMemoryCollection(data).Build(); + + var host = CreateBuilder(config).UseServer(this).Build(); + host.Start(); + Assert.Equal("http://localhost:5003", host.ServerFeatures.Get().Addresses.First()); + } + + [Fact] + public void UsesNewConfigurationOverLegacyConfigForAddresses() + { + var data = new Dictionary + { + { "server.urls", "http://localhost:5003" }, + { "urls", "http://localhost:5009" } + }; + + var config = new ConfigurationBuilder().AddInMemoryCollection(data).Build(); + + var host = CreateBuilder(config).UseServer(this).Build(); + host.Start(); + Assert.Equal("http://localhost:5009", host.ServerFeatures.Get().Addresses.First()); + } + [Fact] public void WebHostCanBeStarted() {