Using a different ServiceProvider
Changing to a different or custom DI container is relatively easy if the other container already supports ASP.NET Core. Usually, the other container will use IServiceCollection
to feed its own container. The third-party DI containers move the already-registered services to the other container by looping over the collection:
- Let's start by using
Autofac
as a third-party container. Type the following command into your command line to load the NuGet package:dotnet add package Autofac.Extensions.DependencyInjection
Autofac
is good for this because you are easily able to see what is happening here.
- To register a custom IoC container, you need to register a different
IServiceProviderFactory
. In that case, you'll want to useAutofacServiceProviderFactory
if you useAutofac
.IServiceProviderFactory
will create aServiceProvider
instance. The third-party container should provide one, if it supports ASP.NET Core.
You should...