WebJob和SignalR集线器之间的通信 [英] Communication between a WebJob and SignalR Hub

查看:84
本文介绍了WebJob和SignalR集线器之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:

  • 我有一个天蓝色的webjob(用于发送邮件),需要在我的Web应用程序中检查webjob的进度.
  • 我正在使用SignalR与服务器上的客户端进行通信.
  • 当我要发送电子邮件时,我在队列中推送一条消息,而天蓝色的webjob会完成他的工作.

问题是,如何将Webjob的进度传达给客户?最初,我的想法是从Webjob推送消息,因此Hub可以从队列中读取它.然后,我将从集线器通知客户.但是,我找不到通信webjob和集线器的方法,我不知道当将消息推送到队列或服务总线中时如何在集线器中触发操作.也就是说,我不知道如何将集线器预订到队列中的某个消息.

The question is, how can I communicate the progress of the webjob to the client? Originally my idea was to push a message from the webjob, so the Hub could read it from the queue. Then, I would notify clients from the hub. However, I am not able to find a way to communicate the webjob and the hub, I do not know how to trigger an action in the hub when a message is pushed into the queue or in the service bus. That is to say, I dont know how to subscribe the hub to a certain message of the queue.

有人可以帮我吗?

推荐答案

我完成此操作的方法是将webjob设置为SignalR客户端,通过SignalR将消息从webjob推送到服务器,然后中继这些消息到SignalR Web客户端.

The way that I have done it is to set up the webjob as a SignalR client, push messages via SignalR from the webjob to the server, then relay those messages to the SignalR web clients.

首先在您的Web作业上安装SignalR Web客户端(程序包ID为Microsoft.AspNet.SignalR.Client).

Start by installing the SignalR web client (nuget package ID is Microsoft.AspNet.SignalR.Client) on your webjob.

然后在您的Web作业中,初始化SignalR连接中心并将消息发送到服务器,例如:

Then in your webjob, initialise your SignalR connection hub and send messages to the server, e.g.:

public class Functions
{
    HubConnection _hub = new HubConnection("http://your.signalr.server");
    var _proxy = hub.CreateHubProxy("EmailHub");

    public async Task ProcessQueueMessageAsync([QueueTrigger("queue")] EmailDto message)
    {
        if (_hub.State == ConnectionState.Disconnected)
        {
            await _hub.Start();
        }

        ...

        await _proxy.Invoke("SendEmailProgress", message.Id, "complete");
    }
}

您的SignalR服务器将接收这些消息,然后将它们中继到其他SignalR客户端,例如:

Your SignalR server will receive these messages and can then relay them to the other SignalR clients, e.g.:

public class EmailHub : Hub
{
    public void SendEmailProgress(int messageId, string status)
    {            
        Clients.All.broadcastEmailStatus(messageId, status);
    }        
}

这篇关于WebJob和SignalR集线器之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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