#358 Support HTTP_PLATFORM_PORT.

This commit is contained in:
Chris R 2015-09-18 12:41:39 -07:00
parent 6442fe8a86
commit fa1896869e
2 changed files with 41 additions and 1 deletions

View File

@ -22,6 +22,9 @@ namespace Microsoft.AspNet.Hosting.Internal
{
public class HostingEngine : IHostingEngine
{
// This is defined by IIS's HttpPlatformHandler.
private static readonly string ServerPort = "HTTP_PLATFORM_PORT";
private readonly IServiceCollection _applicationServiceCollection;
private readonly IStartupLoader _startupLoader;
private readonly ApplicationLifetime _applicationLifetime;
@ -188,6 +191,16 @@ 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<IServerAddressesFeature>()?.Addresses;
if (addresses != null && !addresses.IsReadOnly)
{
addresses.Add(port);
}
}
var startupFilters = _applicationServices.GetService<IEnumerable<IStartupFilter>>();
var configure = Startup.ConfigureDelegate;
foreach (var filter in startupFilters)

View File

@ -5,6 +5,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting.Fakes;
@ -14,6 +15,7 @@ using Microsoft.AspNet.Hosting.Startup;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Http.Features.Internal;
using Microsoft.AspNet.Server.Features;
using Microsoft.AspNet.Testing.xunit;
using Microsoft.Dnx.Runtime.Infrastructure;
using Microsoft.Framework.Configuration;
@ -75,6 +77,24 @@ namespace Microsoft.AspNet.Hosting
Assert.NotNull(host.ApplicationServices.GetRequiredService<IHostingEnvironment>());
}
[Fact]
public void CanSpecifyPortConfig()
{
var vals = new Dictionary<string, string>
{
{ "Hosting:Server", "Microsoft.AspNet.Hosting.Tests" },
{ "HTTP_PLATFORM_PORT", "abc123" }
};
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("abc123", app.ServerFeatures.Get<IServerAddressesFeature>().Addresses.First());
}
[Fact]
public void HostingEngineCanBeStarted()
{
@ -397,7 +417,9 @@ namespace Microsoft.AspNet.Hosting
public IFeatureCollection Initialize(IConfiguration configuration)
{
return null;
var features = new FeatureCollection();
features.Set<IServerAddressesFeature>(new ServerAddressesFeature());
return features;
}
public IDisposable Start(IFeatureCollection serverFeatures, Func<IFeatureCollection, Task> application)
@ -470,5 +492,10 @@ namespace Microsoft.AspNet.Hosting
yield break;
}
}
private class ServerAddressesFeature : IServerAddressesFeature
{
public ICollection<string> Addresses { get; } = new List<string>();
}
}
}