From fc323fceb20e0acb9c71b4610d4f5c222b7e4497 Mon Sep 17 00:00:00 2001 From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com> Date: Sat, 3 Oct 2026 00:43:28 +0100 Subject: [PATCH] fix(aspnetcore): initialize web application factories through services --- src/TUnit.AspNetCore.Core/WebApplicationTest.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/TUnit.AspNetCore.Core/WebApplicationTest.cs b/src/TUnit.AspNetCore.Core/WebApplicationTest.cs index 76d859444c2..31a7ad4862e 100644 --- a/src/TUnit.AspNetCore.Core/WebApplicationTest.cs +++ b/src/TUnit.AspNetCore.Core/WebApplicationTest.cs @@ -10,7 +10,7 @@ namespace TUnit.AspNetCore; public abstract class WebApplicationTest { // Shared across all generic instantiations of WebApplicationTest. - // WebApplicationFactory.Server is synchronous; Task.Run prevents blocking async threads, + // WebApplicationFactory.Services is synchronous; Task.Run prevents blocking async threads, // and this semaphore caps concurrent DI container builds to avoid thread pool starvation. // Capped at 8: startup is reflection/I/O-bound, not CPU-bound, so ProcessorCount alone // would allow too many concurrent builds on high-core-count machines. @@ -127,12 +127,13 @@ public async Task InitializeFactoryAsync(TestContext testContext) (_, config) => ConfigureTestConfiguration(config), ConfigureWebHostBuilder)); - // Semaphore guards only the Server property access (the synchronous host build), + // Services initializes both TestServer and Kestrel; Server only supports TestServer. + // Semaphore guards only the Services property access (the synchronous host build), // not the factory creation above which is fast synchronous configuration. await ServerInitSemaphore.WaitAsync(testContext.Execution.CancellationToken); try { - await Task.Run(() => _ = _factory.Server, testContext.Execution.CancellationToken); + await Task.Run(() => _ = _factory.Services, testContext.Execution.CancellationToken); } finally {