Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,27 @@ private IWebHost CreateKestrelServer(IWebHostBuilder builder)

var host = builder.Build();

if (_kestrelPort is int port)
{
// Captured startup service errors bypass startup filters on the legacy host.
TryConfigureServerPort(GetServerAddressFeature(host), port);
}

host.Start();
return host;
}
#pragma warning restore ASPDEPR008 // IWebHost is obsolete

private static void TryConfigureServerPort(IServerAddressesFeature? serverAddressFeature, int port)
{
if (serverAddressFeature is not null)
{
serverAddressFeature.Addresses.Clear();
serverAddressFeature.Addresses.Add($"http://127.0.0.1:{port}");
serverAddressFeature.PreferHostingUrls = true;
}
}

private void ConfigureBuilderToUseKestrel(IWebHostBuilder builder)
{
if (_configureKestrelOptions is not null)
Expand All @@ -248,13 +264,7 @@ public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
{
// Configure addresses during the app's startup, before Kestrel binds, including
// when a minimal app starts on its own thread.
var serverAddressFeature = server.Features.Get<IServerAddressesFeature>();
if (serverAddressFeature is not null)
{
serverAddressFeature.Addresses.Clear();
serverAddressFeature.Addresses.Add($"http://127.0.0.1:{port}");
serverAddressFeature.PreferHostingUrls = true;
}
TryConfigureServerPort(server.Features.Get<IServerAddressesFeature>(), port);

return next;
}
Expand Down
41 changes: 41 additions & 0 deletions src/Mvc/test/Mvc.FunctionalTests/RealServerPortOverrideTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,34 @@ public async Task PortOverrideIsAppliedWhenStartupFilterShortCircuits()
await AssertDynamicLoopbackEndpoint(factory);
}

[Fact]
public async Task PortOverrideIsAppliedWhenStartupServicesThrow()
{
using var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
var applicationPort = ((IPEndPoint)listener.LocalEndpoint).Port;

await using var factory = new ConfiguredFactory<BasicWebSite.Program>(builder =>
{
builder.UseUrls($"http://127.0.0.1:{applicationPort}")
.CaptureStartupErrors(true)
.UseSetting(WebHostDefaults.DetailedErrorsKey, "true");
builder.ConfigureServices(services => services.AddSingleton<IStartup>(new ThrowingStartup()));
});
factory.UseKestrel(0);

using var client = factory.CreateClient();
var server = factory.Services.GetRequiredService<IServer>();
var address = new Uri(Assert.Single(server.Features.Get<IServerAddressesFeature>()!.Addresses));
Assert.Equal(IPAddress.Loopback.ToString(), address.Host);
Assert.NotEqual(applicationPort, address.Port);
Assert.Equal(address, client.BaseAddress);

using var response = await client.GetAsync("/");
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
Assert.Contains("Startup services failed.", await response.Content.ReadAsStringAsync());
}

private static async Task AssertDynamicLoopbackEndpoint<TEntryPoint>(WebApplicationFactory<TEntryPoint> factory)
where TEntryPoint : class
{
Expand Down Expand Up @@ -128,4 +156,17 @@ private sealed class ShortCircuitStartupFilter : IStartupFilter
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) =>
app => app.Run(_ => Task.CompletedTask);
}

private sealed class ThrowingStartup : IStartup
{
public IServiceProvider ConfigureServices(IServiceCollection services)
{
throw new InvalidOperationException("Startup services failed.");
}

public void Configure(IApplicationBuilder app)
{
throw new InvalidOperationException("Startup configuration should not run.");
}
}
}
Loading