DependencyInjection post-registration support

%3CLINGO-SUB%20id%3D%22lingo-sub-2968945%22%20slang%3D%22en-US%22%3EDependencyInjection%20post-registration%20support%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2968945%22%20slang%3D%22en-US%22%3E%3CP%3E%E6%88%91%E7%8E%B0%E5%9C%A8%E6%9C%89%E4%B8%80%E4%B8%AA%E8%BF%99%E6%A0%B7%E7%9A%84%E9%9C%80%E6%B1%82%EF%BC%9A%3CBR%20%2F%3E%E5%9C%A8%E9%80%9A%E7%94%A8%E4%B8%BB%E6%9C%BA%E7%9A%84%E4%BD%BF%E7%94%A8%E8%BF%87%E7%A8%8B%E4%B8%AD%EF%BC%8C%E9%87%87%E7%94%A8IHostBuilder%E6%89%A9%E5%B1%95%E5%92%8C%E6%89%98%E7%AE%A1%E6%9C%8D%E5%8A%A1(HostedService)%EF%BC%8C%E5%B9%B6%E9%87%87%E7%94%A8%E4%BA%86%E6%8F%92%E4%BB%B6%E6%B3%A8%E5%86%8C%E6%9C%BA%E5%88%B6%E3%80%82%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%E9%A6%96%E5%85%88%EF%BC%8C%E4%BC%9A%E5%B0%86%E4%B8%80%E4%BA%9B%E5%BF%85%E8%A6%81%E7%9A%84%E6%9C%8D%E5%8A%A1%E7%AD%89%EF%BC%8C%E9%80%9A%E8%BF%87%E6%AD%A3%E5%B8%B8%E7%9A%84HostBuilder%E8%A1%8C%E4%B8%BA%E8%BF%9B%E8%A1%8C%E6%B3%A8%E5%86%8C%EF%BC%9B%E7%84%B6%E5%90%8E%E5%9C%A8%E7%8B%AC%E7%AB%8B%E7%9A%84%E6%A8%A1%E5%9D%97%E4%B8%AD%EF%BC%8C%E5%AE%9E%E7%8E%B0%E4%B8%80%E4%B8%AAIHostBuilder%E7%9A%84%E6%89%A9%E5%B1%95%EF%BC%8C%E5%B9%B6%E6%B3%A8%E5%85%A5%E4%B8%80%E4%B8%AAH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BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E7%AD%89%E6%9C%8D%E5%8A%A1%E7%9A%84%E5%88%9B%E5%BB%BA%E5%92%8C%E5%90%AF%E5%8A%A8%E5%B7%A5%E4%BD%9C%E9%83%BD%E5%B7%B2%E7%BB%8F%E5%AE%8C%E6%88%90%E4%BA%86%EF%BC%8C%E8%80%8C%E6%8F%92%E4%BB%B6%E6%89%8D%E5%BC%80%E5%A7%8B%E5%B7%A5%E4%BD%9C%E3%80%82%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%E6%8F%92%E4%BB%B6%E5%8C%85%E5%90%AB%E4%BA%86%E4%B8%89%E4%B8%AA%EF%BC%88InitializeAsync%E3%80%81StartAsync%E3%80%81StopAsync%EF%BC%89%E5%9F%BA%E6%9C%AC%E6%8E%A5%E5%8F%A3%E5%AE%9A%E4%B9%89%EF%BC%8C%E6%88%91%E5%B8%8C%E6%9C%9B%E5%9C%A8InitializeAsync%E6%96%B9%E6%B3%95%E4%B8%AD%E8%83%BD%E5%86%8D%E6%AC%A1%E5%AF%B9%E6%9C%8D%E5%8A%A1%E5%AE%B9%E5%99%A8%E8%BF%9B%E8%A1%8C%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E7%AD%89%E6%93%8D%E4%BD%9C%EF%BC%8C%E6%88%91%E6%9F%A5%E9%98%85%E4%BA%86%E5%B7%B2%E7%9F%A5%E7%9A%84%E5%85%B3%E4%BA%8EMicrosoft.Extensions.DependencyInjection%E7%9A%84%E7%9B%B8%E5%85%B3%E6%96%87%E6%A1%A3%EF%BC%8C%E5%90%8C%E6%97%B6%E4%B9%9F%E9%98%85%E8%AF%BB%E4%BA%86%E9%83%A8%E5%88%86%E6%BA%90%E7%A0%81%EF%BC%8C%E4%BD%86%E4%BB%8D%E6%B2%A1%E8%83%BD%E6%89%BE%E5%88%B0%E5%AE%9E%E7%8E%B0%E8%BF%99%E7%A7%8D%E9%9C%80%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E9%93%BE%E6%8E%A5%EF%BC%9A%3CA%20href%3D%22https%3A%2F%2Fdocs.simpleinjector.org%2Fen%2Flatest%2Fadvanced.html%23unregistered-type-resolution%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.simpleinjector.org%2Fen%2Flatest%2Fadvanced.html%23unregistered-type-resolution%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%E9%9D%9E%E5%B8%B8%E6%84%9F%E8%B0%A2%EF%BC%81%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20now%20have%20a%20need%20for%20such%3A%3CBR%20%2F%3EDuring%20the%20use%20of%20universal%20hosting%2C%20IHostBuilder%20extensions%20and%20hosted%20services%20(HostedService)%20are%20used%2C%20and%20a%20plugin%20registration%20mechanism%20is%20used.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFirst%2C%20some%20necessary%20services%2C%20etc.%2C%20will%20be%20registered%20through%20the%20normal%20HostBuilder%20behavior%3B%20then%20in%20a%20separate%20module%2C%20an%20IHostBuilder%20extension%20will%20be%20implemented%20and%20a%20HostedService%20service%20will%20be%20injected.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20this%20HostedService%20service%2C%20it%20will%20read%20all%20the%20plug-in%20information%2C%20and%20after%20passing%20the%20series%20verification%2C%20it%20will%20reflect%20the%20plug-in%20operation.%3C%2FP%3E%3CP%3EThe%20reflection%20operation%20of%20this%20plug-in%20is%20not%20expected%20to%20affect%20the%20whole%20generic%20host%20startup%20process%20or%20block%20the%20startup%20process%3B%20so%20it%20should%20start%20working%20only%20after%20the%20main%20work%20of%20the%20generic%20host%20part%20is%20finished%2C%20and%20by%20this%20time%20the%20creation%20and%20startup%20of%20services%20such%20as%20dependency%20injection%20are%20finished%2C%20and%20the%20plug-in%20starts%20working.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlugin%20contains%20three%20(InitializeAsync%2C%20StartAsync%2C%20StopAsync)%20basic%20interface%20definitions%2C%20I%20want%20to%20be%20able%20to%20InitializeAsync%20method%20again%20in%20the%20service%20container%20for%20service%20registration%20and%20other%20operations%2C%20I%20checked%20the%20known%20about%20Microsoft.%20DependencyInjection%20documentation%2C%20and%20also%20read%20some%20of%20the%20source%20code%2C%20but%20still%20could%20not%20find%20a%20way%20or%20method%20to%20achieve%20this%20requirement%3F%20Maybe%20my%20mind%20has%20been%20tied%20to%20understanding%20the%20right%20way%20to%20do%20it%2C%20so%20I'm%20here%20for%20help!%20I%20hope%20that%20the%20great%20gods%2C%20experts%20or%20doyens%20who%20know%20will%20give%20answers%20and%20guidance%2C%20thank%20you%20very%20much.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20purpose%20of%20this%20requirement%20is%20that%20throughout%20the%20project%20structure%2C%20some%20public%20interfaces%20will%20be%20defined%2C%20but%20no%20specific%20interface%20implementation%20will%20be%20done%2C%20but%20a%20specific%20implementation%20by%20a%20plugin%2C%20or%20multiple%20plugins%2C%20and%20then%20in%20the%20process%20of%20reflection%2C%20registration%20with%20the%20container%20service%20by%20calling%20InitializeAsync.%20And%20then%2C%20in%20the%20need%20to%20use%20this%20interface%20services%20in%20the%20plug-in%2C%20you%20can%20get%20through%20the%20service%20container%20and%20normal%20use.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAlso%2C%20I%20found%20in%20Simple%20Injector's%20documentation%20about%20a%20similar%20method%20of%20satisfaction%2C%20and%20I%20wonder%20if%20DependencyInjection%20has%20the%20same%20support.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ELink%3A%20%3CA%20href%3D%22https%3A%2F%2Fdocs.simpleinjector.org%2Fen%2Flatest%2Fadvanced.html%23unregistered-type-resolution%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.simpleinjector.org%2Fen%2Flatest%2Fadvanced.html%23unregistered-type-resolution%3C%2FA%3E%3C%2FP%3E%3CP%3E%3CBR%20%2F%3ETranslated%20with%20%3CA%20href%3D%22http%3A%2F%2Fwww.DeepL.com%2FTranslator%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ewww.DeepL.com%2FTranslator%3C%2FA%3E%20(free%20version)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20a%20lot!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2968945%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3E.NET%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3E.Net%20Core%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
New Contributor

我现在有一个这样的需求:
在通用主机的使用过程中,采用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!

0 Replies