如何注入对特定 IHostedService 实现的引用? [英] How to inject a reference to a specific IHostedService implementation?
问题描述
我的网络应用程序有一个后台服务,可以监听服务总线.基于文档,看起来运行后台服务的内置方式是实现IHostedService
.
My web app has a background service that listens to a service bus. Based on the docs, it looks like the built-in way to run a background service is to implement IHostedService
.
所以我有一些看起来像这样的代码:
So I have some code that looks like this:
public class ServiceBusListener : IMessageSource<string>, IHostedService
{
public virtual event ServiceBusMessageHandler<string> OnMessage = delegate { };
public Task StartAsync(CancellationToken cancellationToken)
{
// run the background task...
}
// ... other stuff ...
}
然后在 Startup.cs
中注册该服务:
The service is then registered in Startup.cs
with:
services.AddSingleton<IHostedService, ServiceBusListener>();
更新到 ASP.NET 2.1 后,我可以使用新的便捷方法:
Once I update to ASP.NET 2.1 I can use the new convenience method:
services.AddHostedService<ServiceBusListener>();
但我相信两者在功能上是等效的.
But I believe the two are functionally equivalent.
复杂性:我的网络应用程序有多个 IHostedService
实现(特别是服务总线侦听器的不同实例).
The complication: my web app has multiple implementations of IHostedService
(specifically, different instances of service bus listeners).
问题:我怎样才能让其他组件获得对特定托管服务实现(我的服务总线侦听器)的引用?换句话说,如何将特定实例注入到组件中?
The question: how can I have some other component get a reference to a specific hosted service implementation (my service bus listener)? In other words, how do I get a specific instance injected into a component?
用例:我的后台服务侦听服务总线消息,然后将消息作为 .NET 事件重新发布(如果您想知道,使用代码处理线程问题).如果事件发生在后台服务上,那么订阅者需要获得对后台服务的引用才能订阅.
Use case: my background service listens for service bus messages and then re-publishes messages as .NET events (in case you're wondering, the consuming code deals with the threading issues). If the event is on the background service, then subscribers need to get a reference to the background service to be able to subscribe.
我的尝试:如果我做了显而易见的事情并将 ServiceBusListener
声明为要注入不同组件的依赖项,我的启动代码会抛出Could不解决类型的服务"异常.
What I've tried: if I do the obvious thing and declare ServiceBusListener
as a dependency to be injected into a different component, my startup code throws a "Could not resolve a service of type" exception.
是否可以请求 IHostedService
的特定实现?如果没有,最好的解决方法是什么?引入我的服务和消费者都可以引用的第三个组件?避免 IHostedService
并手动运行后台服务?
Is it even possible to request a specific implementation of a IHostedService
? If not, what's the best workaround? Introduce a third component that both my service and the consumer can reference? Avoid IHostedService
and run the background service manually?
推荐答案
从 .net core 3.1 开始,不需要其他答案中的任何复杂性.如果您不需要从另一个类获得对您的类的具体引用,只需调用:
None of the complexity in the other answers is needed as of .net core 3.1. If you don't need to get a concrete reference to your class from another class, simply call:
services.AddHostedService<MyHostedServiceType>();
如果您必须有具体的参考,请执行以下操作:
If you must have a concrete reference, do the following:
services.AddSingleton<IHostedService, MyHostedServiceType>();
这篇关于如何注入对特定 IHostedService 实现的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!