diff --git a/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs b/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs index 28b72659d2..986def36af 100644 --- a/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs +++ b/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs @@ -191,13 +191,19 @@ namespace Microsoft.AspNet.Hosting.Internal var builder = builderFactory.CreateBuilder(_serverInstance); builder.ApplicationServices = _applicationServices; - var port = _config[ServerPort]; - if (!string.IsNullOrEmpty(port)) + var addresses = builder.ServerFeatures?.Get()?.Addresses; + if (addresses != null && !addresses.IsReadOnly) { - var addresses = builder.ServerFeatures.Get()?.Addresses; - if (addresses != null && !addresses.IsReadOnly) + var port = _config[ServerPort]; + if (!string.IsNullOrEmpty(port)) { - addresses.Add(port); + addresses.Add("http://localhost:" + port); + } + + // Provide a default address if there aren't any configured. + if (addresses.Count == 0) + { + addresses.Add("http://localhost:5000"); } } diff --git a/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs b/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs index 16fbe6149a..ded586de90 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs @@ -92,7 +92,24 @@ namespace Microsoft.AspNet.Hosting var host = CreateBuilder(config).Build(); var app = host.Start(); Assert.NotNull(host.ApplicationServices.GetRequiredService()); - Assert.Equal("abc123", app.ServerFeatures.Get().Addresses.First()); + Assert.Equal("http://localhost:abc123", app.ServerFeatures.Get().Addresses.First()); + } + + [Fact] + public void CanDefaultAddresseIfNotConfigured() + { + var vals = new Dictionary + { + { "Hosting:Server", "Microsoft.AspNet.Hosting.Tests" } + }; + + var builder = new ConfigurationBuilder() + .AddInMemoryCollection(vals); + var config = builder.Build(); + var host = CreateBuilder(config).Build(); + var app = host.Start(); + Assert.NotNull(host.ApplicationServices.GetRequiredService()); + Assert.Equal("http://localhost:5000", app.ServerFeatures.Get().Addresses.First()); } [Fact]