While .net core ships with the service collection dependency injection framework as a first class citizen, some developers may still prefer to stick with their third party DI framework they used in full framework .net. And for good reason, the .net core DI is rather basic when it comes to options for injecting and auto binding. Things like auto binding all interfaces, or being able to inject different services based on parameter names, class names etc are all lost.
Luckily Autofac, LightInject, DryIOC and StructureMap do have the ability to be used in .net core (With others slowly making the move), but there are a couple of gotchas that you need to go through. Namely you have to remember that things like IOptions, ILogger, HttpContext and other framework classes need to be available in the DI framework by default. On top of that, libraries that already use the ServiceCollection of .net core need to be able to be dropped in and used as per normal. яндекс
Almost all third party DI frameworks have adopted the same pattern for .net core. The general steps for any third party DI library are :
- ConfigureServices in your startup.cs file should return IServiceProvider not void
- You should register framework services as you normally would within the .net core service collection
- Build your IOC container and register services as normal
- Push the .net core services collection into the IOC container
- Build your container and return the service provider
Sound complicated? Don’t worry, sample code will be provided for each DI framework as well as any other gotchas you have.
Autofac In ASP.net Core
First install the following Nuget package :
Install-Package Autofac.Extensions.DependencyInjection
Here is the code for your startup.cs :
// Configure Services should now return IServiceProvider public IServiceProvider ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(); //Create your Autofac Container var containerBuilder = new ContainerBuilder(); //Register your own services within Autofac containerBuilder.RegisterType<MyService>().As<IMyService>(); //Put the framework services into Autofac containerBuilder.Populate(services); //Build and return the Autofac collection var container = containerBuilder.Build(); return container.Resolve<IServiceProvider>(); }
DryIOC In ASP.net Core
First install the following Nuget package :
Install-Package DryIoc.Microsoft.DependencyInjection
DryIOC prefers to keep it’s registrations out of the startup.cs file (Which is probably a good idea). So create a class called “CompositionRoot”, that has a constructor that takes IRegistrator, and register your services inside there.
public class CompositionRoot { public CompositionRoot(IRegistrator registrator) { registrator.Register<IMyService, MyService>(); } }
Then your startup.cs should look like the following :
public IServiceProvider ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(); //Return our container, reference our class that holds our registrations. return new Container().WithDependencyInjectionAdapter(services).ConfigureServiceProvider<CompositionRoot>(); }
LightInject In ASP.net Core
First install the following Nuget package :
Install-Package LightInject.Microsoft.DependencyInjection
Here is the code for your startup.cs:
public IServiceProvider ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(); //Create your LightInject container var container = new ServiceContainer(); //Register your own services within LightInject container.Register<IMyService, MyService>(); //Build and return the Service Provider return container.CreateServiceProvider(services); }
StructureMap In ASP.net Core
First install the following Nuget package :
Install-Package StructureMap.Microsoft.DependencyInjection
Note I also had an issue where I had to install StructureMap itself manually for some reason. If things aren’t working correctly (e.g. Intellisense is going wild), install the following Nuget package.
Install-Package StructureMap
StructureMap is another IOC container that prefers a separate class(s) to handle your registrations. Let’s just create a simple ServicesRegistry class.
public class ServicesRegistry : StructureMap.Registry { public ServicesRegistry() { For<IMyService>().Use<MyService>(); } }
In your startup.cs add the following :
public IServiceProvider ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(); //Create our StructureMap container var container = new Container(); container.Configure(config => { //Add in our custom registry config.AddRegistry(new ServicesRegistry()); //Push the .net Core Services Collection into StructureMap config.Populate(services); }); //Return the service provider return container.GetInstance<IServiceProvider>(); }
Unity In ASP.net Core
Unity is close to being dead in the water. It would appear according this Github issue that .net core support won’t be coming any time soon.
Windsor In ASP.net Core
At the time of writing, Windsor does not have support for ASP.net core. You can follow the progress in the Github Issue here.
Ninject In ASP.net Core
At the time of writing, Ninject does not have support for ASP.net core