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

Commit

Permalink
#358 Redo port, add default address.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher committed Sep 22, 2015
1 parent fa18968 commit 285da61
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
16 changes: 11 additions & 5 deletions src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,19 @@ private RequestDelegate BuildApplication()
var builder = builderFactory.CreateBuilder(_serverInstance);
builder.ApplicationServices = _applicationServices;

var port = _config[ServerPort];
if (!string.IsNullOrEmpty(port))
var addresses = builder.ServerFeatures?.Get<IServerAddressesFeature>()?.Addresses;
if (addresses != null && !addresses.IsReadOnly)
{
var addresses = builder.ServerFeatures.Get<IServerAddressesFeature>()?.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");
}
}

Expand Down
19 changes: 18 additions & 1 deletion test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,24 @@ public void CanSpecifyPortConfig()
var host = CreateBuilder(config).Build();
var app = host.Start();
Assert.NotNull(host.ApplicationServices.GetRequiredService<IHostingEnvironment>());
Assert.Equal("abc123", app.ServerFeatures.Get<IServerAddressesFeature>().Addresses.First());
Assert.Equal("http://localhost:abc123", app.ServerFeatures.Get<IServerAddressesFeature>().Addresses.First());
}

[Fact]
public void CanDefaultAddresseIfNotConfigured()
{
var vals = new Dictionary<string, string>
{
{ "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<IHostingEnvironment>());
Assert.Equal("http://localhost:5000", app.ServerFeatures.Get<IServerAddressesFeature>().Addresses.First());
}

[Fact]
Expand Down

0 comments on commit 285da61

Please sign in to comment.