Get service from WebApplicationFactory in ASP.NET Core integration tests

You need to create a scope from service provider to get necessary service:

using (var scope = AppFactory.Server.Host.Services.CreateScope())
{
    var context = scope.ServiceProvider.GetRequiredService<MyDatabaseContext>();
}

Leave a Comment