Background and Motivation
See #37680 for the background and motivation. This is highly requested user feature.
Proposed API
namespace Microsoft.AspNetCore.Mvc.Testing;
public class WebApplicationFactory<TEntryPoint> : IDisposable, IAsyncDisposable where TEntryPoint : class
{
+ protected virtual void ConfigureHostApplicationBuilder(IHostApplicationBuilder hostApplicationBuilder);
}
Open questions:
- What instance should be passed to
IHostApplicationBuilder? The WebApplicationBuilder or its inner _hostApplicationBuilder? Does it matter that much?
Usage Examples
public class TestServerFixture : WebApplicationFactory<Program>
{
internal Dictionary<string, string> OverrideConfiguration = new();
protected override void ConfigureHostApplicationBuilder(IHostApplicationBuilder hostApplicationBuilder)
{
base.ConfigureHostApplicationBuilder(hostApplicationBuilder);
var configuration = hostApplicationBuilder.Configuration;
var testDir = Path.GetDirectoryName(GetType().Assembly.Location);
var configLocation = Path.Combine(testDir!, "testsettings.json");
configuration.Sources.Clear();
configuration.AddJsonFile(configLocation);
configuration.AddInMemoryCollection(OverrideConfiguration);
}
}
Alternative Designs
Risks
Background and Motivation
See #37680 for the background and motivation. This is highly requested user feature.
Proposed API
namespace Microsoft.AspNetCore.Mvc.Testing; public class WebApplicationFactory<TEntryPoint> : IDisposable, IAsyncDisposable where TEntryPoint : class { + protected virtual void ConfigureHostApplicationBuilder(IHostApplicationBuilder hostApplicationBuilder); }ConfigureHostApplicationBuildershould be called during the entrypoint's call toWebApplication.CreateBuilder.ConfigureHostApplicationBuildervirtual method on WebApplicationFactory to allow early configuration of the host application builder #66527 is an implementation of this proposal.WebApplicationBuilder. The draft PR above doesn't have this yet and we need to figure out the best way to do that.Open questions:
IHostApplicationBuilder? TheWebApplicationBuilderor its inner_hostApplicationBuilder? Does it matter that much?Usage Examples
Alternative Designs
Risks