Nov 16 2021 12:18 AM
我现在有一个这样的需求:
在通用主机的使用过程中,采用IHostBuilder扩展和托管服务(HostedService),并采用了插件注册机制。
首先,会将一些必要的服务等,通过正常的HostBuilder行为进行注册;然后在独立的模块中,实现一个IHostBuilder的扩展,并注入一个HostedService服务。
在这个HostedService服务中,会读取所有的插件信息,并通过系列验证后,会对插件进行反射操作。
这个插件的反射操作,不希望它影响到整个通用主机的启动过程,或者阻塞启动过程;所以它应该是在通用主机部分主要工作完成后,才会开始工作,而这个时候如依赖注入等服务的创建和启动工作都已经完成了,而插件才开始工作。
插件包含了三个(InitializeAsync、StartAsync、StopAsync)基本接口定义,我希望在InitializeAsync方法中能再次对服务容器进行服务的注册等操作,我查阅了已知的关于Microsoft.Extensions.DependencyInjection的相关文档,同时也阅读了部分源码,但仍没能找到实现这种需求的方式或方法?可能是我的思维已经被束缚,没能理解到正确的做法,所以,特来此寻求帮助!望知道的大神、专家或道友给予回答和指导,非常感谢。
这个需求的目的在于,在整个项目结构中,会定义一些公共的接口,但不会做具体的接口实现,而是由某个插件,或者多个插件进行具体的实现,然后在反射的过程,通过调用InitializeAsync来向容器服务进行注册。而后,在需要使用此接口服务的插件中,可以通过服务容器获取到并正常使用。
另外,我在Simple Injector的文档中找到了关于类似满足的方法,不知道DependencyInjection是否有同样的支持。
链接:https://docs.simpleinjector.org/en/latest/advanced.html#unregistered-type-resolution
非常感谢!
I now have a need for such:
During the use of universal hosting, IHostBuilder extensions and hosted services (HostedService) are used, and a plugin registration mechanism is used.
First, some necessary services, etc., will be registered through the normal HostBuilder behavior; then in a separate module, an IHostBuilder extension will be implemented and a HostedService service will be injected.
In this HostedService service, it will read all the plug-in information, and after passing the series verification, it will reflect the plug-in operation.
The reflection operation of this plug-in is not expected to affect the whole generic host startup process or block the startup process; so it should start working only after the main work of the generic host part is finished, and by this time the creation and startup of services such as dependency injection are finished, and the plug-in starts working.
Plugin contains three (InitializeAsync, StartAsync, StopAsync) basic interface definitions, I want to be able to InitializeAsync method again in the service container for service registration and other operations, I checked the known about Microsoft. DependencyInjection documentation, and also read some of the source code, but still could not find a way or method to achieve this requirement? Maybe my mind has been tied to understanding the right way to do it, so I'm here for help! I hope that the great gods, experts or doyens who know will give answers and guidance, thank you very much.
The purpose of this requirement is that throughout the project structure, some public interfaces will be defined, but no specific interface implementation will be done, but a specific implementation by a plugin, or multiple plugins, and then in the process of reflection, registration with the container service by calling InitializeAsync. And then, in the need to use this interface services in the plug-in, you can get through the service container and normal use.
Also, I found in Simple Injector's documentation about a similar method of satisfaction, and I wonder if DependencyInjection has the same support.
Link: https://docs.simpleinjector.org/en/latest/advanced.html#unregistered-type-resolution
Translated with www.DeepL.com/Translator (free version)
Thanks a lot!