Signalr-Core Hub中带有参数的构造函数 [英] constructor with parameters in a signalr-core hub
本文介绍了Signalr-Core Hub中带有参数的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在集线器中注入一些东西.
I would like to inject something into my hub.
基本上,我正在尝试等效于本教程 https://docs.microsoft.com/zh-cn/aspnet/signalr/overview/advanced/dependency-injection ,但适用于SignalR-Core.我对这部分最感兴趣
Basically I am trying to the equivalent of this tutorial https://docs.microsoft.com/en-us/aspnet/signalr/overview/advanced/dependency-injection, but for SignalR-Core. I am mostly interested in the part
public void Configuration(IAppBuilder app)
{
GlobalHost.DependencyResolver.Register(
typeof(ChatHub),
() => new ChatHub(new ChatMessageRepository()));
App.MapSignalR();
// ...
}
我该如何使用Net Core和SignalR-Core?
How do I do this Net Core and SignalR-Core?
推荐答案
在DI容器中使用以下方法注册您的 ChatMessageRepository
:
Register your ChatMessageRepository
in the DI container with:
services.AddTransient(typeof(ChatMessageRepository), typeof(ChatMessageRepository));
,然后在ctor中注入您的集线器:
and then inject into your hub in the ctor:
public ChatHub : Hub
{
private readonly ChatMessageRepository _repository;
public ChatHub(ChatMessageRepository repository)
{
_repository = repository;
}
...
}
这篇关于Signalr-Core Hub中带有参数的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文