细节
我试图使用ASP.NET 2.1中推荐的IHostedService
接口(interface)创建后台处理结构。我注册服务如下:
services.AddSingleton<AbstractProcessQueue<AbstractImportProcess>>();
services.AddHostedService<AbstractBackgroundProcessService<AbstractImportProcess>>();
services.AddSignalR();
AbstractProcessQueue
只是可被入队和出队的进程的BlockingCollection
的包装。 AbstractBackgroundProcessService
实现IHostedService
接口(interface),并在队列中查找可以启动的新进程。现在,当我尝试在
SignalR
中心内尝试通过Dependency Injection
机制获取对后台处理服务的引用时,麻烦就开始了。我已经尝试了以下解决方案,但似乎都无法按预期工作:选项1:
public HubImportClient(IServiceProvider provider)
{
//This returns null.
var service = provider.GetService<AbstractBackgroundProcessService<AbstractImportProcess>>();
}
选项2:
public HubImportClient(IServiceProvider provider)
{
//This returns null.
var service = (AbstractBackgroundProcessService<AbstractImportProcess>) provider.GetService(typeof(AbstractBackgroundProcessService<AbstractImportProcess>>));
}
选项3:
public HubImportClient(IServiceProvider provider)
{
//This throws an exception, because the service is missing.
var service = provider.GetRequiredService<AbstractBackgroundProcessService<AbstractImportProcess>>();
}
选项4:
public HubImportClient(IServiceProvider provider)
{
//This throws an exception, because the service is missing.
var service = (AbstractBackgroundProcessService<AbstractImportProcess>) provider.GetRequiredService(typeof(AbstractBackgroundProcessService<AbstractImportProcess>);
}
选项5:
public HubImportClient(IServiceProvider provider)
{
//This returns a correct service, but prevents me from adding additional AbstractBackgroundProcessService implementations with different type parameters.
//Additionally, it seems like this reference was newly created, and not the instance that was created on application startup (i.e. the hash codes are different, and the constructor is called an additional time).
var service = provider.GetService<IHostedService>();
if(service is AbstractBackgroundProcessService<AbstractProcessService>)
{ this.Service = (AbstractBackgroundProcessService<AbstractProcessService>) service;}
}
选项6:
public HubImportClient(IServiceProvider provider)
{
//This works similarly to the previous option, and allows multiple implementations, but the constructor is still called twice and the instances thus differ.
AbstractBackgroundProcessService<AbstractImportProcess> service = null;
foreach(IHostedService service in provider.GetServices<IHostedService>())
{
if(service is AbstractBackgroundProcessService<AbstractImportProcess>)
{
service = (AbstractBackgroundProcessService<AbstractImportProcess>) service;
break;
}
}
}
选项7:
public HubImportClient(IServiceProvider provider)
{
//This just skips the for each loop all together, because no such services could be found.
AbstractBackgroundProcessService<AbstractImportProcess> service = null;
foreach(AbstractBackgroundProcessService<AbstractImportProcess> current in provider.GetServices<AbstractBackgroundProcessService<AbstractImportProcess> >())
{
service = current;
break;
}
}
选项8:
//This works, but prevents multiple implementations again.
public HubImportClient(IHostedService service)
{
this.Service = service;
}
选项9:
//This does not work again.
public HubImportClient(AbstractBackgroundProcessService<AbstractImportProcess> service)
{
this.Service = service;
}
问题
因此,我的问题仍然存在:我应该如何获得对
IHostedService
实现的引用,以便:(a):我可以注入(inject)服务的多个实例,这些实例的唯一区别在于其类型参数(例如
AbstractImportProcess
es的托管服务以及AbstractExportProcess
es的托管服务)(b):该特定类型参数只有
IHostedService
的一个实例。在此先感谢您的帮助!
最佳答案
围绕此主题进行了一些讨论。例如,请参见:https://github.com/aspnet/Hosting/issues/1489。您将遇到的问题之一是将托管服务添加为临时服务(来自ASP.NET Core 2.1+),这意味着从依赖项注入(inject)容器中解析托管服务每次都会导致一个新实例。
一般建议是将要与其他服务共享或从其他服务交互的任何业务逻辑封装到特定服务中。查看代码,建议您在AbstractProcessQueue<AbstractImportProcess>
类中实现业务逻辑,并使执行业务逻辑成为AbstractBackgroundProcessService<T>
的唯一关注点。