如何将 IServiceCollection 注册转换为 Autofac 注册?

How to convert IServiceCollection registrations to Autofac registrations?

很棒 Microsoft.Extensions.DependencyInjection.IServiceCollection 有很多相关的扩展。

一个例子是 Microsoft.Extensions.Logging.ConsoleLoggerExtensionsAddConsole(this ILoggingBuilder builder) 方法,它在 IServiceCollection.[=18= 类型的 ILoggingBuilder.Services 属性 中注册 ConsoleLoggerProvider ]

所以,对于任何使用 Autofac 的人来说,这是一个合乎逻辑的问题。如何重新使用所有这些扩展?有没有办法将所有 IServiceCollection 注册转换为 ContainerBuilder 注册?

获得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.

顺便说一下,这种适配器模式并不是 Autofac 特有的。这种转换是大多数其他 DI 框架如何支持 Microsoft.Extensions.DependencyInjection 中的一致容器的方式。如果您出于某种原因碰巧离开了 Autofac,请查看有关这些框架的文档,因为它们都有某种集成库。