Using SignalR with Azure Service Fabric

2019-01-28 06:48发布

问题:

Is it possible to use signalR with Service Fabric provided by Microsoft? I'm trying to connect an UWP app and an angularJS app to my stateless reliable service hosted in an Azure Cluster through signalR/websocket connection. But both are not able to open such a connection. On my local machine everything works fine.

Is there anything special to consider of using signalR/websockets with Service Fabric? Is there any example?

回答1:

You need to configure the load balancer to the client ip. Then the user will always get to the same node and other users to other nodes. The user to node relationship will be persistent.



回答2:

You need to enable cross-domain setting for signalR to call it from other applications.

 appBuilder.Map("/signalr", map =>
        {

            map.UseCors(CorsOptions.AllowAll);
            var hubConfiguration = new HubConfiguration()
            {
                EnableJSONP = true,
                EnableDetailedErrors = true,
                EnableJavaScriptProxies  = true
            };

            map.RunSignalR(hubConfiguration);
        });

This code can be found in the readme.txt file we get after installing the Nuget package for the SignalR. For cross domain feature, you need to install the Cores nuget package also.

After deploying this application, you can see that some calls to signalR fails and Some succeeds. By default the instance count in Reliable stateless service is -1. Thus multiple instances will be created for the service.

From signaleR Connection.Start() method three SingalR APIs will be called. These calls should happen within the same session in-order to work it properly. When we call these three API in Service Fabric with multiple instances, the call might be processed in different nodes and thus different sessions which leads to failure in singalR connection.

One solution is to Make the instance count 1 for the stateless service fabric application. I don't think that this is an elegant solution.