constructor with parameters in a signalr-core hub

南笙酒味 提交于 2019-12-11 08:45:53

问题


I would like to inject something into my hub.

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();

    // ...
}

How do I do this Net Core and SignalR-Core?


回答1:


Register your ChatMessageRepository in the DI container with:

services.AddTransient(typeof(ChatMessageRepository), typeof(ChatMessageRepository));

and then inject into your hub in the ctor:

public ChatHub : Hub
{
    private readonly ChatMessageRepository _repository;
    public ChatHub(ChatMessageRepository repository)
    {
        _repository = repository;
    }
    ...
}


来源:https://stackoverflow.com/questions/46406269/constructor-with-parameters-in-a-signalr-core-hub

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!