简体   繁体   中英

Invoking SignalR from Azure Function

I'm trying to set up SignalR with my Azure system. The setup is fairly standard

  • Main web portal in Azure App Service written in ASP.NET MVC
  • A cloud service worker role that performs business logic. The worker role listens on a queue that the portal writes to. I had to use a cloud service for this due to a reliance on legacy C++ business logic

When a user starts a time consuming operation from the portal, I want the cloud service to be able to inform all the connected browsers via SignalR that the cloud service is complete.

I thought I could do this using an Azure Function like a SignalR client, that could be triggered to force SignalR to broadcast a message using code in an Azure function like this

    HubConnection hub = new HubConnection(*URL OF PORTAL GOES HERE*);

    var proxy = hub.CreateHubProxy(*NAME OF HUB GOES HERE*);
    await hub.Start();
    await proxy.Invoke("Hello"); // Name of default hub method

ASP.NET MVC side, I'm also using Azure Service Bus queues, which seems to be the advice when it comes to scaling. So, in my Startup.cs I've got

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            GlobalHost.DependencyResolver.UseServiceBus(@"*SERVICE BUS CONNNECTION STRING GOES HERE*", "blah");
            app.MapSignalR();
        }
    }

Note: In order to get this to work with the latest SignalR I had to use the Microsoft.AspNet.SignalR.ServiceBus3 nuget package otherwise I just got an exception when the UseServiceBus call was made.

Now, it all appears to work. I can call the Azure function from PostMan and sure enough in my browser the signalr call comes through.

However, I'm not sure if this is the right thing to do. Will scaling work in this case or is there a better way of forcing signalr to broadcast messages out?

thanks

You've got the right idea. I am running the same architecture, except I'm using a WebJob instead of an Azure Function. A few points:

  • It's a good choice to go with the Service Bus backplane for SignalR. I've been running it with some fairly high volumes, and it performs well. Another developer I've chatted with here on StackOverflow went the Redis backplane route and had lots of problems.
  • You've got the right approach with creating a proxy in your Azure Function. However, establishing a HubConnection is a heavyweight operation. For performance, consider creating the HubConnection and proxy once, then reusing. You will need to make sure that you handle the case where SignalR loses the connection to your Hub, exhausts retries, then gives up. If you are reusing the HubConnection and this happens, you're in trouble if you don't have logic to attempt to re-establish the connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM