diff --git a/src/Microsoft.AspNetCore.Server.HttpSys/MessagePump.cs b/src/Microsoft.AspNetCore.Server.HttpSys/MessagePump.cs index 6fd6d3051a..01b8d831e8 100644 --- a/src/Microsoft.AspNetCore.Server.HttpSys/MessagePump.cs +++ b/src/Microsoft.AspNetCore.Server.HttpSys/MessagePump.cs @@ -2,11 +2,9 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Threading; using System.Threading.Tasks; -using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Hosting.Server.Features; using Microsoft.AspNetCore.Http.Features; @@ -69,24 +67,39 @@ namespace Microsoft.AspNetCore.Server.HttpSys throw new ArgumentNullException(nameof(application)); } - var serverAdressesPresent = _serverAddresses.Addresses.Count > 0; + var hostingUrlsPresent = _serverAddresses.Addresses.Count > 0; - if (_options.UrlPrefixes.Count > 0) + if (_serverAddresses.PreferHostingUrls && hostingUrlsPresent) { - if (serverAdressesPresent) + if (_options.UrlPrefixes.Count > 0) + { + LogHelper.LogWarning(_logger, $"Overriding endpoints added to {nameof(HttpSysOptions.UrlPrefixes)} since {nameof(IServerAddressesFeature.PreferHostingUrls)} is set to true." + + $" Binding to address(es) '{string.Join(", ", _serverAddresses.Addresses)}' instead. "); + + Listener.Options.UrlPrefixes.Clear(); + } + + foreach (var value in _serverAddresses.Addresses) + { + Listener.Options.UrlPrefixes.Add(value); + } + } + else if (_options.UrlPrefixes.Count > 0) + { + if (hostingUrlsPresent) { LogHelper.LogWarning(_logger, $"Overriding address(es) '{string.Join(", ", _serverAddresses.Addresses)}'. " + $"Binding to endpoints added to {nameof(HttpSysOptions.UrlPrefixes)} instead."); _serverAddresses.Addresses.Clear(); + } - foreach (var prefix in _options.UrlPrefixes) - { - _serverAddresses.Addresses.Add(prefix.FullPrefix); - } + foreach (var prefix in _options.UrlPrefixes) + { + _serverAddresses.Addresses.Add(prefix.FullPrefix); } } - else if (serverAdressesPresent) + else if (hostingUrlsPresent) { foreach (var value in _serverAddresses.Addresses) { diff --git a/test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/MessagePumpTests.cs b/test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/MessagePumpTests.cs index ccbc964607..48913a746f 100644 --- a/test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/MessagePumpTests.cs +++ b/test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/MessagePumpTests.cs @@ -12,6 +12,63 @@ namespace Microsoft.AspNetCore.Server.HttpSys { public class MessagePumpTests { + [ConditionalFact] + public void OverridingDirectConfigurationWithIServerAddressesFeatureSucceeds() + { + var serverAddress = "http://localhost:11001/"; + var overrideAddress = "http://localhost:11002/"; + + using (var server = new MessagePump(Options.Create(new HttpSysOptions()), new LoggerFactory())) + { + var serverAddressesFeature = server.Features.Get(); + serverAddressesFeature.Addresses.Add(overrideAddress); + serverAddressesFeature.PreferHostingUrls = true; + server.Listener.Options.UrlPrefixes.Add(serverAddress); + + server.Start(new DummyApplication()); + + Assert.Equal(overrideAddress, serverAddressesFeature.Addresses.Single()); + } + } + + [ConditionalTheory] + [InlineData("http://localhost:11001/")] + [InlineData("invalid address")] + [InlineData("")] + [InlineData(null)] + public void DoesNotOverrideDirectConfigurationWithIServerAddressesFeature_IfPreferHostinUrlsFalse(string overrideAddress) + { + var serverAddress = "http://localhost:11002/"; + + using (var server = new MessagePump(Options.Create(new HttpSysOptions()), new LoggerFactory())) + { + var serverAddressesFeature = server.Features.Get(); + serverAddressesFeature.Addresses.Add(overrideAddress); + server.Listener.Options.UrlPrefixes.Add(serverAddress); + + server.Start(new DummyApplication()); + + Assert.Equal(serverAddress, serverAddressesFeature.Addresses.Single()); + } + } + + [ConditionalFact] + public void DoesNotOverrideDirectConfigurationWithIServerAddressesFeature_IfAddressesIsEmpty() + { + var serverAddress = "http://localhost:11002/"; + + using (var server = new MessagePump(Options.Create(new HttpSysOptions()), new LoggerFactory())) + { + var serverAddressesFeature = server.Features.Get(); + serverAddressesFeature.PreferHostingUrls = true; + server.Listener.Options.UrlPrefixes.Add(serverAddress); + + server.Start(new DummyApplication()); + + Assert.Equal(serverAddress, serverAddressesFeature.Addresses.Single()); + } + } + [ConditionalTheory] [InlineData("http://localhost:11001/")] [InlineData("invalid address")]