Support ASPNETCORE_URLS to set server urls
- Read both urls and server.urls in WebHost - UseUrls now sets urls instead of server.urls
This commit is contained in:
parent
14557f0131
commit
e7b8c3f90a
|
|
@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Hosting
|
||||||
public static readonly string EnvironmentKey = "environment";
|
public static readonly string EnvironmentKey = "environment";
|
||||||
public static readonly string WebRootKey = "webroot";
|
public static readonly string WebRootKey = "webroot";
|
||||||
public static readonly string CaptureStartupErrorsKey = "captureStartupErrors";
|
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";
|
public static readonly string ContentRootKey = "contentRoot";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,8 @@ namespace Microsoft.AspNetCore.Hosting.Internal
|
||||||
{
|
{
|
||||||
public class WebHost : IWebHost
|
public class WebHost : IWebHost
|
||||||
{
|
{
|
||||||
|
private static readonly string DeprecatedServerUrlsKey = "server.urls";
|
||||||
|
|
||||||
private readonly IServiceCollection _applicationServiceCollection;
|
private readonly IServiceCollection _applicationServiceCollection;
|
||||||
private IStartup _startup;
|
private IStartup _startup;
|
||||||
|
|
||||||
|
|
@ -188,7 +190,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal
|
||||||
var addresses = Server.Features?.Get<IServerAddressesFeature>()?.Addresses;
|
var addresses = Server.Features?.Get<IServerAddressesFeature>()?.Addresses;
|
||||||
if (addresses != null && !addresses.IsReadOnly && addresses.Count == 0)
|
if (addresses != null && !addresses.IsReadOnly && addresses.Count == 0)
|
||||||
{
|
{
|
||||||
var urls = _config[WebHostDefaults.ServerUrlsKey];
|
var urls = _config[WebHostDefaults.ServerUrlsKey] ?? _config[DeprecatedServerUrlsKey];
|
||||||
if (!string.IsNullOrEmpty(urls))
|
if (!string.IsNullOrEmpty(urls))
|
||||||
{
|
{
|
||||||
foreach (var value in urls.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
|
foreach (var value in urls.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@ using System.Reflection;
|
||||||
using System.Runtime.ExceptionServices;
|
using System.Runtime.ExceptionServices;
|
||||||
using Microsoft.AspNetCore.Hosting.Builder;
|
using Microsoft.AspNetCore.Hosting.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting.Internal;
|
using Microsoft.AspNetCore.Hosting.Internal;
|
||||||
using Microsoft.AspNetCore.Hosting.Server;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
@ -47,14 +46,8 @@ namespace Microsoft.AspNetCore.Hosting
|
||||||
?? Environment.GetEnvironmentVariable("Hosting:Environment")
|
?? Environment.GetEnvironmentVariable("Hosting:Environment")
|
||||||
?? Environment.GetEnvironmentVariable("ASPNET_ENV"));
|
?? Environment.GetEnvironmentVariable("ASPNET_ENV"));
|
||||||
|
|
||||||
if (Environment.GetEnvironmentVariable("Hosting:Environment") != null)
|
// Add the default server.urls key
|
||||||
{
|
UseSetting(WebHostDefaults.ServerUrlsKey, Environment.GetEnvironmentVariable("ASPNETCORE_URLS"));
|
||||||
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'");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -133,6 +126,17 @@ namespace Microsoft.AspNetCore.Hosting
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IWebHost Build()
|
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 hostingServices = BuildHostingServices();
|
||||||
var hostingContainer = hostingServices.BuildServiceProvider();
|
var hostingContainer = hostingServices.BuildServiceProvider();
|
||||||
|
|
||||||
|
|
@ -192,7 +196,7 @@ namespace Microsoft.AspNetCore.Hosting
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var startupType = StartupLoader.FindStartupType(_options.StartupAssembly, _hostingEnvironment.EnvironmentName);
|
var startupType = StartupLoader.FindStartupType(_options.StartupAssembly, _hostingEnvironment.EnvironmentName);
|
||||||
|
|
||||||
if (typeof(IStartup).GetTypeInfo().IsAssignableFrom(startupType.GetTypeInfo()))
|
if (typeof(IStartup).GetTypeInfo().IsAssignableFrom(startupType.GetTypeInfo()))
|
||||||
{
|
{
|
||||||
services.AddSingleton(typeof(IStartup), startupType);
|
services.AddSingleton(typeof(IStartup), startupType);
|
||||||
|
|
|
||||||
|
|
@ -75,6 +75,52 @@ namespace Microsoft.AspNetCore.Hosting
|
||||||
Assert.Equal("http://localhost:5000", host.ServerFeatures.Get<IServerAddressesFeature>().Addresses.First());
|
Assert.Equal("http://localhost:5000", host.ServerFeatures.Get<IServerAddressesFeature>().Addresses.First());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UsesLegacyConfigurationForAddresses()
|
||||||
|
{
|
||||||
|
var data = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ "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<IServerAddressesFeature>().Addresses.First());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UsesConfigurationForAddresses()
|
||||||
|
{
|
||||||
|
var data = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ "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<IServerAddressesFeature>().Addresses.First());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UsesNewConfigurationOverLegacyConfigForAddresses()
|
||||||
|
{
|
||||||
|
var data = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ "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<IServerAddressesFeature>().Addresses.First());
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void WebHostCanBeStarted()
|
public void WebHostCanBeStarted()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue