diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration/WebHostBuilderIISExtensions.cs b/src/Microsoft.AspNetCore.Server.IISIntegration/WebHostBuilderIISExtensions.cs index c8b2da50c0..39ba3aefdf 100644 --- a/src/Microsoft.AspNetCore.Server.IISIntegration/WebHostBuilderIISExtensions.cs +++ b/src/Microsoft.AspNetCore.Server.IISIntegration/WebHostBuilderIISExtensions.cs @@ -20,27 +20,28 @@ namespace Microsoft.AspNetCore.Hosting /// Configures the port and base path the server should listen on when running behind AspNetCoreModule. /// The app will also be configured to capture startup errors. /// - /// + /// /// - public static IWebHostBuilder UseIISIntegration(this IWebHostBuilder app) + public static IWebHostBuilder UseIISIntegration(this IWebHostBuilder hostBuilder) { - if (app == null) + if (hostBuilder == null) { - throw new ArgumentNullException(nameof(app)); + throw new ArgumentNullException(nameof(hostBuilder)); } - var port = app.GetSetting(ServerPort) ?? Environment.GetEnvironmentVariable($"ASPNETCORE_{ServerPort}"); - var path = app.GetSetting(ServerPath) ?? Environment.GetEnvironmentVariable($"ASPNETCORE_{ServerPath}"); - var pairingToken = app.GetSetting(PairingToken) ?? Environment.GetEnvironmentVariable($"ASPNETCORE_{PairingToken}"); + var port = hostBuilder.GetSetting(ServerPort) ?? Environment.GetEnvironmentVariable($"ASPNETCORE_{ServerPort}"); + var path = hostBuilder.GetSetting(ServerPath) ?? Environment.GetEnvironmentVariable($"ASPNETCORE_{ServerPath}"); + var pairingToken = hostBuilder.GetSetting(PairingToken) ?? Environment.GetEnvironmentVariable($"ASPNETCORE_{PairingToken}"); if (!string.IsNullOrEmpty(port) && !string.IsNullOrEmpty(path) && !string.IsNullOrEmpty(pairingToken)) { var address = "http://localhost:" + port + path; - app.UseSetting(WebHostDefaults.ServerUrlsKey, address); - app.CaptureStartupErrors(true); + hostBuilder.CaptureStartupErrors(true); - app.ConfigureServices(services => + hostBuilder.ConfigureServices(services => { + // Delay register the url so users don't accidently overwrite it. + hostBuilder.UseSetting(WebHostDefaults.ServerUrlsKey, address); services.AddSingleton(new IISSetupFilter(pairingToken)); services.Configure(options => { @@ -56,7 +57,7 @@ namespace Microsoft.AspNetCore.Hosting }); } - return app; + return hostBuilder; } } } diff --git a/test/Microsoft.AspNetCore.Server.IISIntegration.Tests/IISMiddlewareTests.cs b/test/Microsoft.AspNetCore.Server.IISIntegration.Tests/IISMiddlewareTests.cs index be1aacf8b6..f2e49fb97c 100644 --- a/test/Microsoft.AspNetCore.Server.IISIntegration.Tests/IISMiddlewareTests.cs +++ b/test/Microsoft.AspNetCore.Server.IISIntegration.Tests/IISMiddlewareTests.cs @@ -72,6 +72,23 @@ namespace Microsoft.AspNetCore.Server.IISIntegration Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); } + [Fact] + public void UrlDelayRegistered() + { + var builder = new WebHostBuilder() + .UseSetting("TOKEN", "TestToken") + .UseSetting("PORT", "12345") + .UseSetting("APPL_PATH", "/") + .UseIISIntegration(); + + Assert.Null(builder.GetSetting(WebHostDefaults.ServerUrlsKey)); + + // Adds a server and calls Build() + var server = new TestServer(builder); + + Assert.Equal("http://localhost:12345/", builder.GetSetting(WebHostDefaults.ServerUrlsKey)); + } + [Fact] public async Task AddsAuthenticationHandlerByDefault() {