创建集线器实例SignalR 3 [英] Create hub instance SignalR 3

查看:69
本文介绍了创建集线器实例SignalR 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图创建一个Hub实例,以便A可以在所有客户端上调用方法.在Signalr 2中,我会用过.

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

但这似乎在Signalr 3中丢失了,我尝试了以下操作,但出现错误.

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.

有人知道如何在SignalR 3中完成吗?

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

我正在使用signalr3 rc1

I am using signalr3 rc1

推荐答案

您可以使用依赖项注入来解析实例;

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
    }
}

或手动;

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

这篇关于创建集线器实例SignalR 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆