Search code examples
c#asp.netazuresignalrsignalr-backplane

Asp.net Azure SignalR service on a distributed system


I have a web app running on Asp.net 4.7.2. This app contains a few REST endpoints that are queried and then delegate work off to a windows service that sits on another machine.

When the work on this service is complete, I want to use SignalR to send a message to the client to indicate that the work is done. Because this service is on another machine, this is proving quite tricky.

I have tried using an Azure SignalR Service as an abstracted level above this. My intention was to use the REST capabilities to call this Azure service, have it run code from a Hub (which I have currently defined and created in the web app) and then broadcast a message to the clients.

At the moment, I am not sure if this is possible. The notes say that the REST provision is only available in the asp.net CORE version of the library. I have made calls to the endpoint and received an accepted response, but no luck.

My question is, then, how do I get the following architecture working under my conditions? If I cannot, what other suggestions do you have?

Machine 1:

  • Windows service running bespoke code that takes an unpredictable length of time

  • When code completes, send message to SignalR hub via azure service

Machine 2:

  • Webapp containing the SignalR hub definitions and client logic.

Solution

  • You can actually accomplish this without the service using just your existing webapp. The key is to create an endpoint that your Windows service to call that is linked to your hub.

    So if I create a simple SingalR hub in .Net 4.x:

    public class NotificationHub : Hub
    {
        public void Send(string message)
        {
            Clients.All.addNewMessageToPage(name, message);
        }
    }
    

    I can access it in a WebApi controller:

    public class NotificationController : ApiController
    {
        IHubContext context;
    
        public NotificationController()
        {
            context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
        }
    
        public HttpResponseMessage Get(string message)
        {
            object[] args = { message };
            context.Clients.All.Send("Send", args) ;
    
            return Request.CreateResponse(HttpStatusCode.OK);
        }
    }
    

    Using a REST endpoint is going to be simplest in frameworks like MVC and WebApi, but it is possible to add a messaging service in between like Service Bus if you need to do more than simply return a message to the clients.