Create hub instance SignalR 3

自作多情 提交于 2020-01-07 05:27:12

问题


I am tring to create an instance of a Hub so that a can call a method on all of the clients. In Signalr 2 I would have used.

GlobalHost.ConnectionManager.GetHubContext<Hub>();

But this seems to be missing in Signalr 3 I tried the following but I get an error.

IHubActivator.Create
Using a Hub instance not created by the HubPipeline is unsupported.

Does anybody know how this can be accomplished in SignalR 3?

I am using signalr3 rc1


回答1:


You can resolve the instance using dependency injection;

public void MyController : Controller 
{
    public MyController(IHubContext<MyHub, IMyClient> context)
    {
        context.Clients.All.MyMethod("Hi there!"); // strongly typed 
    }

    // or

    public MyController(IHubContext<MyHub> context)
    {
        context.Clients.All.MyMethod("Hi there!"); // dynamic
    }
}

Or manually;

public void Configure(IApplicationBuilder app) 
{
    var context = app.ApplicationServices.GetRequiredService<IHubContext<MyHub>>();
    context.Clients.All.MyMethod("Hi there!");
}


来源:https://stackoverflow.com/questions/34245312/create-hub-instance-signalr-3

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