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

Add PreferHostingUrls to WebHostBuilder and IServerAddressesFeature #992

Merged
merged 1 commit into from
Mar 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; }
Copy link
Contributor

@cesarblum cesarblum Mar 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it settable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hosting needs to set it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the concrete type not the interface.

Copy link
Contributor

@cesarblum cesarblum Mar 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed this. The problem is that the feature is set by the server. Hosting sets it on the interface. Would have to cast to do that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see

}
}
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"
}
]
Whitespace-only changes.
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
7 changes: 4 additions & 3 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 @@ -46,6 +45,8 @@ public WebHostOptions(IConfiguration configuration)

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