从控制器调用 SignalR Core Hub 方法 [英] Call SignalR Core Hub method from Controller

查看:53
本文介绍了从控制器调用 SignalR Core Hub 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从控制器调用 SignalR Core Hub 方法?
我将 ASP.NET Core 2.0 与 Microsoft.AspNetCore.SignalR (1.0.0-alpha2-final) 结合使用.

How can I call SignalR Core Hub method from Controller?
I am using ASP.NET Core 2.0 with Microsoft.AspNetCore.SignalR (1.0.0-alpha2-final).

我有与 Excel、SolidEdge 通信的 Windows 服务......操作完成后,它会在 ASP.NET Core 应用程序中向我的控制器发送请求.现在我需要通知所有使用 SignalR 连接到服务器的客户端外部程序完成了一些任务.
我无法改变窗口服务的工作方式.(无法从窗口服务连接到 SignalR).
我为旧的 SignalR (GlobalHost.ConnectionManager.GetHubContext) 找到了很多解决方案,但已经发生了很大变化,这些解决方案不再有效.

I have windows service which communicate with Excel, SolidEdge ... When operation is complete it post request to my controller in ASP.NET Core application. Now I need to inform all clients connected to server with SignalR that external program completed some task.
I can not change the way window service works. (Can not connect to SignalR from window service).
I found plenty solution for old SignalR (GlobalHost.ConnectionManager.GetHubContext), but much has changed and those solutions are not working anymore.

我的控制器:

[Route("API/vardesigncomm")]
public class VarDesignCommController : Controller
{
    [HttpPut("ProcessVarDesignCommResponse/{id}")]
    public async Task<IActionResult> ProcessVarDesignCommResponse(int id)
    {
        //call method TaskCompleted in Hub !!!! How?

        return new JsonResult(true);
    }
}

我的中心:

public class VarDesignHub : Hub
{
    public async Task TaskCompleted(int id)
    {
        await Clients.All.InvokeAsync("Completed", id);
    }
}

推荐答案

解决方案 1

另一种可能性是将您的 HubContext 注入您的控制器,例如:

Another possibility is to inject your HubContext into your controller like:

public VarDesignCommController(IHubContext<VarDesignHub> hubcontext)
{
    HubContext = hubcontext;
    ...
}

private IHubContext<VarDesignHub> HubContext
{ get; set; }

那你也可以打电话

await this.HubContext.Clients.All.InvokeAsync("Completed", id);

但是你将在所有客户端上直接调用方法.

But then you will direct call methods on all clients.

解决方案 2

您还可以使用类型集线器:简单地创建一个接口,您可以在其中定义服务器可以在客户端上调用哪些方法:

You can also work with typed hubs: Simple create an interface where you define which methods your server can call on the clients:

public interface ITypedHubClient
{
    Task BroadcastMessage(string name, string message);
}

从集线器继承:

public class ChatHub : Hub<ITypedHubClient>
{
    public void Send(string name, string message)
    {
        Clients.All.BroadcastMessage(name, message);
    }
}

将输入的 hubcontext 注入控制器,并使用它:

Inject your the typed hubcontext into your controller, and work with it:

[Route("api/demo")]
public class DemoController : Controller
{
    IHubContext<ChatHub, ITypedHubClient> _chatHubContext;
    public DemoController(IHubContext<ChatHub, ITypedHubClient> chatHubContext)
    {
        _chatHubContext = chatHubContext;
    }

    // GET: api/values
    [HttpGet]
    public IEnumerable<string> Get()
    {
        _chatHubContext.Clients.All.BroadcastMessage("test", "test");
        return new string[] { "value1", "value2" };
    }
}

这篇关于从控制器调用 SignalR Core Hub 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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