Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert IServiceCollection registrations to Autofac registrations?

Wonderful Microsoft.Extensions.DependencyInjection.IServiceCollection has a lot of extensions associated with it.

One example is Microsoft.Extensions.Logging.ConsoleLoggerExtensions and AddConsole(this ILoggingBuilder builder) method which registers ConsoleLoggerProvider in ILoggingBuilder.Services property of type IServiceCollection.

So, the logical question for anyone using Autofac. How to re-use all this extensions? Is there's a way to convert all IServiceCollection registrations to ContainerBuilder registrations?

like image 777
astef Avatar asked Sep 18 '25 07:09

astef


1 Answers

Getting Microsoft.Extensions.DependencyInjection registrations into Autofac is the whole point of the Autofac.Extensions.DependencyInjection package. That's the integration for Autofac with .NET Core. There is plenty of doc and examples on how to get going.

By the way, this sort of adapter pattern isn't specific to Autofac. This conversion sort of thing is how most of the other DI frameworks back the conforming container in Microsoft.Extensions.DependencyInjection. If you happen to switch away from Autofac for some reason, check out the docs on those frameworks because they all have some sort of integration library.

like image 155
Travis Illig Avatar answered Sep 20 '25 22:09

Travis Illig