I have successfully converted my web applications to start using the new WebApplication.CreateBuilder(args) method, but I am not managing to replicate this success in my tests using a custom (derived) WebApplicationFactory.
I decided to keep a loose concept of a Startup class in my codebase (due to inheritance and what not), but I now call it manually as per https://mcmap.net/q/590861/-how-to-use-startup-class-inside-program-file-on-net-6.
My code used to work like this:
public class TestWebApiFactory<TStartup> : WebApplicationFactory<TStartup>
where TStartup : class
{
public TestWebApiFactory()
{
}
protected override void ConfigureClient(HttpClient client)
{
base.ConfigureClient(client);
}
protected override IHostBuilder CreateHostBuilder()
{
var builder = Host.CreateDefaultBuilder()
.ConfigureAppConfiguration((context, config) =>
{
config.AddJsonFile("appsettings.json");
})
.ConfigureWebHostDefaults(x =>
{
x.UseStartup<TStartup>().UseTestServer();
});
return builder;
}
}
How do I achieve the same thing using the WebApplication model instead?
Startup
in the new (minimal) hosting model is just an POCO class with no special meaning. – AscribeStartup
classes at all (that is a bit pointless in the minimal hosting model)? HowProgram
will select correct startup class to use? Use the same mechanism to make theWebApplicationFactory
's (I assume something with configuration). – AscribeTestWebApiFactory
as desired. – Strongroom