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

Commit

Permalink
Server addresses configuration enhancements
Browse files Browse the repository at this point in the history
Add PreferHostingUrls to IServerAdressesFeature

Add extension to IWebHostBuilder to set this flag
  • Loading branch information
JunTaoLuo committed Mar 30, 2017
1 parent f15c99c commit 4cdc970
Show file tree
Hide file tree
Showing 11 changed files with 299 additions and 190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,18 @@ public static IWebHostBuilder UseUrls(this IWebHostBuilder hostBuilder, params s
return hostBuilder.UseSetting(WebHostDefaults.ServerUrlsKey, string.Join(ServerUrlsSeparator, urls));
}

/// <summary>
/// Indicate whether the host should listen on the URLs configured on the <see cref="IWebHostBuilder"/>
/// instead of those configured on the <see cref="IServer"/>.
/// </summary>
/// <param name="hostBuilder">The <see cref="IWebHostBuilder"/> to configure.</param>
/// <param name="preferHostingUrls"><c>true</c> to prefer URLs configured on the <see cref="IWebHostBuilder"/>; otherwise <c>false</c>.</param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
public static IWebHostBuilder PreferHostingUrls(this IWebHostBuilder hostBuilder, bool preferHostingUrls)
{
return hostBuilder.UseSetting(WebHostDefaults.PreferHostingUrls, preferHostingUrls ? "true" : "false");
}

/// <summary>
/// Start the web host and listen on the specified urls.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public static class WebHostDefaults
public static readonly string CaptureStartupErrorsKey = "captureStartupErrors";
public static readonly string ServerUrlsKey = "urls";
public static readonly string ContentRootKey = "contentRoot";
public static readonly string PreferHostingUrls = "preferHostingUrls";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ namespace Microsoft.AspNetCore.Hosting.Server.Features
public interface IServerAddressesFeature
{
ICollection<string> Addresses { get; }

bool PreferHostingUrls { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"OldTypeId": "public interface Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature",
"NewTypeId": "public interface Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature",
"NewMemberId": "System.Boolean get_PreferHostingUrls()",
"Kind": "Addition"
},
{
"OldTypeId": "public interface Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature",
"NewTypeId": "public interface Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature",
"NewMemberId": "System.Void set_PreferHostingUrls(System.Boolean value)",
"Kind": "Addition"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"OldTypeId": "public interface Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature",
"NewTypeId": "public interface Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature",
"NewMemberId": "System.Boolean get_PreferHostingUrls()",
"Kind": "Addition"
},
{
"OldTypeId": "public interface Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature",
"NewTypeId": "public interface Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature",
"NewMemberId": "System.Void set_PreferHostingUrls(System.Boolean value)",
"Kind": "Addition"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static void Initialize(this IHostingEnvironment hostingEnvironment, strin
{
hostingEnvironment.WebRootFileProvider = new NullFileProvider();
}

hostingEnvironment.EnvironmentName =
options.Environment ??
hostingEnvironment.EnvironmentName;
Expand Down
5 changes: 4 additions & 1 deletion src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,15 @@ private void EnsureServer()
{
Server = _applicationServices.GetRequiredService<IServer>();

var addresses = Server.Features?.Get<IServerAddressesFeature>()?.Addresses;
var serverAddressesFeature = Server.Features?.Get<IServerAddressesFeature>();
var addresses = serverAddressesFeature?.Addresses;
if (addresses != null && !addresses.IsReadOnly && addresses.Count == 0)
{
var urls = _config[WebHostDefaults.ServerUrlsKey] ?? _config[DeprecatedServerUrlsKey];
if (!string.IsNullOrEmpty(urls))
{
serverAddressesFeature.PreferHostingUrls = _options.PreferHostingUrls;

foreach (var value in urls.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
{
addresses.Add(value);
Expand Down
9 changes: 5 additions & 4 deletions src/Microsoft.AspNetCore.Hosting/Internal/WebHostOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal
{
public class WebHostOptions
{
public WebHostOptions()
{
}
public WebHostOptions() { }

public WebHostOptions(IConfiguration configuration)
{
Expand All @@ -28,6 +26,7 @@ public WebHostOptions(IConfiguration configuration)
WebRoot = configuration[WebHostDefaults.WebRootKey];
ContentRootPath = configuration[WebHostDefaults.ContentRootKey];
HostingStartupAssemblies = configuration[WebHostDefaults.HostingStartupAssembliesKey]?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries) ?? new string[0];
PreferHostingUrls = ParseBool(configuration, WebHostDefaults.PreferHostingUrls);
}

public string ApplicationName { get; set; }
Expand All @@ -39,13 +38,15 @@ public WebHostOptions(IConfiguration configuration)
public bool CaptureStartupErrors { get; set; }

public string Environment { get; set; }

public string StartupAssembly { get; set; }

public string WebRoot { get; set; }

public string ContentRootPath { get; set; }

public bool PreferHostingUrls { get; set; }

private static bool ParseBool(IConfiguration configuration, string key)
{
return string.Equals("true", configuration[key], StringComparison.OrdinalIgnoreCase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ namespace Microsoft.AspNetCore.Hosting.Server.Features
public class ServerAddressesFeature : IServerAddressesFeature
{
public ICollection<string> Addresses { get; } = new List<string>();

public bool PreferHostingUrls { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public void ReadsParametersCorrectly()
{ "startupAssembly", "MyProjectReference" },
{ "environment", "Development"},
{ "detailederrors", "true"},
{ "captureStartupErrors", "true" }
{ "captureStartupErrors", "true" },
{ "preferHostingUrls", "true" }
};

var config = new WebHostOptions(new ConfigurationBuilder().AddInMemoryCollection(parameters).Build());
Expand All @@ -31,6 +32,7 @@ public void ReadsParametersCorrectly()
Assert.Equal("Development", config.Environment);
Assert.True(config.CaptureStartupErrors);
Assert.True(config.DetailedErrors);
Assert.True(config.PreferHostingUrls);
}

[Fact]
Expand Down
Loading

0 comments on commit 4cdc970

Please sign in to comment.