#242 Delay registering the url until Build.

This commit is contained in:
Chris R 2016-08-15 14:17:29 -07:00
parent ffaa2cf460
commit e94db64619
2 changed files with 29 additions and 11 deletions

View File

@ -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.
/// </summary>
/// <param name="app"></param>
/// <param name="hostBuilder"></param>
/// <returns></returns>
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<IStartupFilter>(new IISSetupFilter(pairingToken));
services.Configure<ForwardedHeadersOptions>(options =>
{
@ -56,7 +57,7 @@ namespace Microsoft.AspNetCore.Hosting
});
}
return app;
return hostBuilder;
}
}
}

View File

@ -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()
{