Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit e7b8c3f

Browse files
committed
Support ASPNETCORE_URLS to set server urls
- Read both urls and server.urls in WebHost - UseUrls now sets urls instead of server.urls
1 parent 14557f0 commit e7b8c3f

4 files changed

Lines changed: 64 additions & 12 deletions

File tree

src/Microsoft.AspNetCore.Hosting.Abstractions/WebHostDefaults.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static class WebHostDefaults
1212
public static readonly string EnvironmentKey = "environment";
1313
public static readonly string WebRootKey = "webroot";
1414
public static readonly string CaptureStartupErrorsKey = "captureStartupErrors";
15-
public static readonly string ServerUrlsKey = "server.urls";
15+
public static readonly string ServerUrlsKey = "urls";
1616
public static readonly string ContentRootKey = "contentRoot";
1717
}
1818
}

src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ namespace Microsoft.AspNetCore.Hosting.Internal
2020
{
2121
public class WebHost : IWebHost
2222
{
23+
private static readonly string DeprecatedServerUrlsKey = "server.urls";
24+
2325
private readonly IServiceCollection _applicationServiceCollection;
2426
private IStartup _startup;
2527

@@ -188,7 +190,7 @@ private void EnsureServer()
188190
var addresses = Server.Features?.Get<IServerAddressesFeature>()?.Addresses;
189191
if (addresses != null && !addresses.IsReadOnly && addresses.Count == 0)
190192
{
191-
var urls = _config[WebHostDefaults.ServerUrlsKey];
193+
var urls = _config[WebHostDefaults.ServerUrlsKey] ?? _config[DeprecatedServerUrlsKey];
192194
if (!string.IsNullOrEmpty(urls))
193195
{
194196
foreach (var value in urls.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))

src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using System.Runtime.ExceptionServices;
1010
using Microsoft.AspNetCore.Hosting.Builder;
1111
using Microsoft.AspNetCore.Hosting.Internal;
12-
using Microsoft.AspNetCore.Hosting.Server;
1312
using Microsoft.AspNetCore.Http;
1413
using Microsoft.Extensions.Configuration;
1514
using Microsoft.Extensions.DependencyInjection;
@@ -47,14 +46,8 @@ public WebHostBuilder()
4746
?? Environment.GetEnvironmentVariable("Hosting:Environment")
4847
?? Environment.GetEnvironmentVariable("ASPNET_ENV"));
4948

50-
if (Environment.GetEnvironmentVariable("Hosting:Environment") != null)
51-
{
52-
Console.WriteLine("The environment variable 'Hosting:Environment' is obsolete and has been replaced with 'ASPNETCORE_ENVIRONMENT'");
53-
}
54-
if (Environment.GetEnvironmentVariable("ASPNET_ENV") != null)
55-
{
56-
Console.WriteLine("The environment variable 'ASPNET_ENV' is obsolete and has been replaced with 'ASPNETCORE_ENVIRONMENT'");
57-
}
49+
// Add the default server.urls key
50+
UseSetting(WebHostDefaults.ServerUrlsKey, Environment.GetEnvironmentVariable("ASPNETCORE_URLS"));
5851
}
5952

6053
/// <summary>
@@ -133,6 +126,17 @@ public IWebHostBuilder ConfigureLogging(Action<ILoggerFactory> configureLogging)
133126
/// </summary>
134127
public IWebHost Build()
135128
{
129+
// Warn about deprecated environment variables
130+
if (Environment.GetEnvironmentVariable("Hosting:Environment") != null)
131+
{
132+
Console.WriteLine("The environment variable 'Hosting:Environment' is obsolete and has been replaced with 'ASPNETCORE_ENVIRONMENT'");
133+
}
134+
135+
if (Environment.GetEnvironmentVariable("ASPNET_ENV") != null)
136+
{
137+
Console.WriteLine("The environment variable 'ASPNET_ENV' is obsolete and has been replaced with 'ASPNETCORE_ENVIRONMENT'");
138+
}
139+
136140
var hostingServices = BuildHostingServices();
137141
var hostingContainer = hostingServices.BuildServiceProvider();
138142

@@ -192,7 +196,7 @@ private IServiceCollection BuildHostingServices()
192196
try
193197
{
194198
var startupType = StartupLoader.FindStartupType(_options.StartupAssembly, _hostingEnvironment.EnvironmentName);
195-
199+
196200
if (typeof(IStartup).GetTypeInfo().IsAssignableFrom(startupType.GetTypeInfo()))
197201
{
198202
services.AddSingleton(typeof(IStartup), startupType);

test/Microsoft.AspNetCore.Hosting.Tests/WebHostTests.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,52 @@ public void CanDefaultAddressesIfNotConfigured()
7575
Assert.Equal("http://localhost:5000", host.ServerFeatures.Get<IServerAddressesFeature>().Addresses.First());
7676
}
7777

78+
[Fact]
79+
public void UsesLegacyConfigurationForAddresses()
80+
{
81+
var data = new Dictionary<string, string>
82+
{
83+
{ "server.urls", "http://localhost:5002" }
84+
};
85+
86+
var config = new ConfigurationBuilder().AddInMemoryCollection(data).Build();
87+
88+
var host = CreateBuilder(config).UseServer(this).Build();
89+
host.Start();
90+
Assert.Equal("http://localhost:5002", host.ServerFeatures.Get<IServerAddressesFeature>().Addresses.First());
91+
}
92+
93+
[Fact]
94+
public void UsesConfigurationForAddresses()
95+
{
96+
var data = new Dictionary<string, string>
97+
{
98+
{ "urls", "http://localhost:5003" }
99+
};
100+
101+
var config = new ConfigurationBuilder().AddInMemoryCollection(data).Build();
102+
103+
var host = CreateBuilder(config).UseServer(this).Build();
104+
host.Start();
105+
Assert.Equal("http://localhost:5003", host.ServerFeatures.Get<IServerAddressesFeature>().Addresses.First());
106+
}
107+
108+
[Fact]
109+
public void UsesNewConfigurationOverLegacyConfigForAddresses()
110+
{
111+
var data = new Dictionary<string, string>
112+
{
113+
{ "server.urls", "http://localhost:5003" },
114+
{ "urls", "http://localhost:5009" }
115+
};
116+
117+
var config = new ConfigurationBuilder().AddInMemoryCollection(data).Build();
118+
119+
var host = CreateBuilder(config).UseServer(this).Build();
120+
host.Start();
121+
Assert.Equal("http://localhost:5009", host.ServerFeatures.Get<IServerAddressesFeature>().Addresses.First());
122+
}
123+
78124
[Fact]
79125
public void WebHostCanBeStarted()
80126
{

0 commit comments

Comments
 (0)