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

Commit 4cdc970

Browse files
committed
Server addresses configuration enhancements
Add PreferHostingUrls to IServerAdressesFeature Add extension to IWebHostBuilder to set this flag
1 parent f15c99c commit 4cdc970

File tree

11 files changed

+299
-190
lines changed

11 files changed

+299
-190
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,18 @@ public static IWebHostBuilder UseUrls(this IWebHostBuilder hostBuilder, params s
145145
return hostBuilder.UseSetting(WebHostDefaults.ServerUrlsKey, string.Join(ServerUrlsSeparator, urls));
146146
}
147147

148+
/// <summary>
149+
/// Indicate whether the host should listen on the URLs configured on the <see cref="IWebHostBuilder"/>
150+
/// instead of those configured on the <see cref="IServer"/>.
151+
/// </summary>
152+
/// <param name="hostBuilder">The <see cref="IWebHostBuilder"/> to configure.</param>
153+
/// <param name="preferHostingUrls"><c>true</c> to prefer URLs configured on the <see cref="IWebHostBuilder"/>; otherwise <c>false</c>.</param>
154+
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
155+
public static IWebHostBuilder PreferHostingUrls(this IWebHostBuilder hostBuilder, bool preferHostingUrls)
156+
{
157+
return hostBuilder.UseSetting(WebHostDefaults.PreferHostingUrls, preferHostingUrls ? "true" : "false");
158+
}
159+
148160
/// <summary>
149161
/// Start the web host and listen on the specified urls.
150162
/// </summary>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ public static class WebHostDefaults
1515
public static readonly string CaptureStartupErrorsKey = "captureStartupErrors";
1616
public static readonly string ServerUrlsKey = "urls";
1717
public static readonly string ContentRootKey = "contentRoot";
18+
public static readonly string PreferHostingUrls = "preferHostingUrls";
1819
}
1920
}

src/Microsoft.AspNetCore.Hosting.Server.Abstractions/Features/IServerAddressesFeature.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ namespace Microsoft.AspNetCore.Hosting.Server.Features
88
public interface IServerAddressesFeature
99
{
1010
ICollection<string> Addresses { get; }
11+
12+
bool PreferHostingUrls { get; set; }
1113
}
1214
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"OldTypeId": "public interface Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature",
4+
"NewTypeId": "public interface Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature",
5+
"NewMemberId": "System.Boolean get_PreferHostingUrls()",
6+
"Kind": "Addition"
7+
},
8+
{
9+
"OldTypeId": "public interface Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature",
10+
"NewTypeId": "public interface Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature",
11+
"NewMemberId": "System.Void set_PreferHostingUrls(System.Boolean value)",
12+
"Kind": "Addition"
13+
}
14+
]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"OldTypeId": "public interface Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature",
4+
"NewTypeId": "public interface Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature",
5+
"NewMemberId": "System.Boolean get_PreferHostingUrls()",
6+
"Kind": "Addition"
7+
},
8+
{
9+
"OldTypeId": "public interface Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature",
10+
"NewTypeId": "public interface Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature",
11+
"NewMemberId": "System.Void set_PreferHostingUrls(System.Boolean value)",
12+
"Kind": "Addition"
13+
}
14+
]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static void Initialize(this IHostingEnvironment hostingEnvironment, strin
6060
{
6161
hostingEnvironment.WebRootFileProvider = new NullFileProvider();
6262
}
63-
63+
6464
hostingEnvironment.EnvironmentName =
6565
options.Environment ??
6666
hostingEnvironment.EnvironmentName;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,15 @@ private void EnsureServer()
249249
{
250250
Server = _applicationServices.GetRequiredService<IServer>();
251251

252-
var addresses = Server.Features?.Get<IServerAddressesFeature>()?.Addresses;
252+
var serverAddressesFeature = Server.Features?.Get<IServerAddressesFeature>();
253+
var addresses = serverAddressesFeature?.Addresses;
253254
if (addresses != null && !addresses.IsReadOnly && addresses.Count == 0)
254255
{
255256
var urls = _config[WebHostDefaults.ServerUrlsKey] ?? _config[DeprecatedServerUrlsKey];
256257
if (!string.IsNullOrEmpty(urls))
257258
{
259+
serverAddressesFeature.PreferHostingUrls = _options.PreferHostingUrls;
260+
258261
foreach (var value in urls.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
259262
{
260263
addresses.Add(value);

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal
99
{
1010
public class WebHostOptions
1111
{
12-
public WebHostOptions()
13-
{
14-
}
12+
public WebHostOptions() { }
1513

1614
public WebHostOptions(IConfiguration configuration)
1715
{
@@ -28,6 +26,7 @@ public WebHostOptions(IConfiguration configuration)
2826
WebRoot = configuration[WebHostDefaults.WebRootKey];
2927
ContentRootPath = configuration[WebHostDefaults.ContentRootKey];
3028
HostingStartupAssemblies = configuration[WebHostDefaults.HostingStartupAssembliesKey]?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries) ?? new string[0];
29+
PreferHostingUrls = ParseBool(configuration, WebHostDefaults.PreferHostingUrls);
3130
}
3231

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

4140
public string Environment { get; set; }
42-
41+
4342
public string StartupAssembly { get; set; }
4443

4544
public string WebRoot { get; set; }
4645

4746
public string ContentRootPath { get; set; }
4847

48+
public bool PreferHostingUrls { get; set; }
49+
4950
private static bool ParseBool(IConfiguration configuration, string key)
5051
{
5152
return string.Equals("true", configuration[key], StringComparison.OrdinalIgnoreCase)

src/Microsoft.AspNetCore.Hosting/Server/Features/ServerAddressesFeature.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ namespace Microsoft.AspNetCore.Hosting.Server.Features
88
public class ServerAddressesFeature : IServerAddressesFeature
99
{
1010
public ICollection<string> Addresses { get; } = new List<string>();
11+
12+
public bool PreferHostingUrls { get; set; }
1113
}
1214
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public void ReadsParametersCorrectly()
2020
{ "startupAssembly", "MyProjectReference" },
2121
{ "environment", "Development"},
2222
{ "detailederrors", "true"},
23-
{ "captureStartupErrors", "true" }
23+
{ "captureStartupErrors", "true" },
24+
{ "preferHostingUrls", "true" }
2425
};
2526

2627
var config = new WebHostOptions(new ConfigurationBuilder().AddInMemoryCollection(parameters).Build());
@@ -31,6 +32,7 @@ public void ReadsParametersCorrectly()
3132
Assert.Equal("Development", config.Environment);
3233
Assert.True(config.CaptureStartupErrors);
3334
Assert.True(config.DetailedErrors);
35+
Assert.True(config.PreferHostingUrls);
3436
}
3537

3638
[Fact]

0 commit comments

Comments
 (0)