Communication between a WebJob and SignalR Hub

人盡茶涼 提交于 2019-12-03 12:56:49

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.

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

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");
    }
}

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