4

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?

astef
  • 8,575
  • 4
  • 56
  • 95
  • That's not the logical question. It's actually the other way around. If you are using Microsoft.Extensions.DependencyInjection, you should register all your AutoFac components with it, and then use it to resolve your dependencies. – mason Nov 16 '18 at 13:22
  • @mason I'm using Autofac with all features available in it. Sorry, it's not a sample application, I can't change IoC container to be able to use those extensions. Just wondering, if there's already an easy way to do it – astef Nov 16 '18 at 13:27
  • What features are you using that makes you not able to migrate? – mason Nov 16 '18 at 13:28
  • Check out my last question if you're interested: https://stackoverflow.com/questions/53318662/how-to-build-a-graph-of-resolved-instances-with-autofac – astef Nov 16 '18 at 13:29

1 Answers1

7

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.

Bakudan
  • 19,134
  • 9
  • 53
  • 73
Travis Illig
  • 23,195
  • 2
  • 62
  • 85