This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Support ASPNETCORE_URLS to set server urls

- Read both urls and server.urls in WebHost
- UseUrls now sets urls instead of server.urls
  • Loading branch information...
davidfowl committed May 21, 2016
1 parent 14557f0 commit e7b8c3f90a781a55658b4245571c5bf5dac5e56e
@@ -12,7 +12,7 @@ public static class WebHostDefaults
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";
}
}
@@ -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 @@ private void EnsureServer()
var addresses = Server.Features?.Get<IServerAddressesFeature>()?.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))
@@ -9,7 +9,6 @@
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 @@ public WebHostBuilder()
?? 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"));
}

/// <summary>
@@ -133,6 +126,17 @@ public IWebHostBuilder ConfigureLogging(Action<ILoggerFactory> configureLogging)
/// </summary>
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 @@ private IServiceCollection BuildHostingServices()
try
{
var startupType = StartupLoader.FindStartupType(_options.StartupAssembly, _hostingEnvironment.EnvironmentName);

if (typeof(IStartup).GetTypeInfo().IsAssignableFrom(startupType.GetTypeInfo()))
{
services.AddSingleton(typeof(IStartup), startupType);
@@ -75,6 +75,52 @@ public void CanDefaultAddressesIfNotConfigured()
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]
public void WebHostCanBeStarted()
{

0 comments on commit e7b8c3f

Please sign in to comment.