在 AspnetCore 中使用 IServiceCollection 注册第三方容器是否错误

Is it wrong to register a 3rd party container with IServiceCollection in AspnetCore

一点背景知识:我开始将 FluentValidation 合并到一个 AspnetCore 应用程序中,我在其中使用 SimpleInjector 作为我选择的 DI。根据 aspnet 文档

"The ConfigureServices method typically returns void, but if its signature is changed to return IServiceProvider, a different container can be configured and returned"

这一切都很好,直到我意识到我需要配置不是我的服务,这就是我想该示例使用 containerBuilder.Populate(services); 的原因。 FluentValidation 的好处是我可以创建自己的 IValidatorFactory 来解决我的验证器(这是我的问题所在,因为我的验证器是在 SimpleInjector 上注册的,而不是我认为我有 none).我想我可以通过在 IServiceCollection 中注册 SimpleInjector 的 container 并将其作为 IServiceProvider 注入我的 IValidatorFactory 而不是返回 [=19 中的容器来解决这个问题=] 方法。看起来这一切都像黑客一样有效,但这是要走的路吗?除了aspnet docs wiki还有其他方法吗?

引用https://github.com/JeremySkinner/FluentValidation/blob/master/src/FluentValidation.AspNetCore/ServiceProviderValidatorFactory.cs

https://docs.asp.net/en/latest/fundamentals/dependency-injection.html

Simple Injector 与 ASP.NET 核心 DI 抽象之间存在根本的不兼容性(即无法解决的不兼容性),并且许多 problems in general 与 Microsoft 提出的抽象不兼容。

因为这个our official advice是为了:

Refrain from using a self-developed or 3rd party provided adapter for the .NET Core DI abstraction. Isolate the registration of application components from the framework and 3rd party components. Pursue a SOLID way of working and allow your application registrations to be verified and diagnosed by Simple Injector, without concern for incompatibilities with the framework and 3rd party components.

您可以详细阅读 here 在使用第 3 方容器时应如何使用 ASP.NET Core。