In my class, StartupI use a method ConfigureServices(IServiceCollection services)to configure my service container using the built-in DI container from Microsoft.Extensions.DependencyInjection.
I want to check the dependency graph in unit test to verify that all services can be constructed so that I can fix any services that are missing during unit testing, instead of the application crashing during application execution. In previous projects, I used Simple Injector, which has a method .Verify()for the container. But I could not find anything like it for ASP.NET Core.
Is there a built-in (or at least recommended) way to verify that you can build an entire dependency graph?
(The stupidest way I can think of is something like this, but it will still fail due to open generics that are introduced by the framework itself):
startup.ConfigureServices(serviceCollection);
var provider = serviceCollection.BuildServiceProvider();
foreach (var serviceDescriptor in serviceCollection)
{
provider.GetService(serviceDescriptor.ServiceType);
}
source
share