From fa1896869ef77089baaa5716c6e1624014b8b379 Mon Sep 17 00:00:00 2001 From: Chris R Date: Fri, 18 Sep 2015 12:41:39 -0700 Subject: [PATCH] #358 Support HTTP_PLATFORM_PORT. --- .../Internal/HostingEngine.cs | 13 +++++++++ .../HostingEngineTests.cs | 29 ++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs b/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs index aa68a00689..28b72659d2 100644 --- a/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs +++ b/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs @@ -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()?.Addresses; + if (addresses != null && !addresses.IsReadOnly) + { + addresses.Add(port); + } + } + var startupFilters = _applicationServices.GetService>(); var configure = Startup.ConfigureDelegate; foreach (var filter in startupFilters) diff --git a/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs b/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs index b831277232..16fbe6149a 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs @@ -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()); } + [Fact] + public void CanSpecifyPortConfig() + { + var vals = new Dictionary + { + { "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()); + Assert.Equal("abc123", app.ServerFeatures.Get().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(new ServerAddressesFeature()); + return features; } public IDisposable Start(IFeatureCollection serverFeatures, Func application) @@ -470,5 +492,10 @@ namespace Microsoft.AspNet.Hosting yield break; } } + + private class ServerAddressesFeature : IServerAddressesFeature + { + public ICollection Addresses { get; } = new List(); + } } }