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);
}
ConfigureHostApplicationBuilder should be called during the entrypoint's call to WebApplication.CreateBuilder.
#66527 is an implementation of this proposal.
Open questions:
- What instance should be passed to
IHostApplicationBuilder? The WebApplicationBuilder or its inner _hostApplicationBuilder? Does it matter that much?
- What are all different ways to implement the entrypoint? Do they all create
WebApplicationBuilder? Do they all create HostApplicationBuilder? Do we care about any entrypoint that doesn't create those types?
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.#66527 is an implementation of this proposal.
Open questions:
IHostApplicationBuilder? TheWebApplicationBuilderor its inner_hostApplicationBuilder? Does it matter that much?WebApplicationBuilder? Do they all createHostApplicationBuilder? Do we care about any entrypoint that doesn't create those types?Usage Examples
Alternative Designs
Risks