.NET Core SignalR Automatic Reconnects

One of the major differences between old and new .NET Core SignalR is that automatic reconnects are not supported. This means that you need explicitly open a new connection if you lost connection to the server.

I managed to write my own implementation that do automatic reconnects on this new version of .NET Core 2 SignalR that works for me.

The code bellow uses Polly for automatic retry policy and Serilog for logging.

Here is my implementation. Hope someone will find it useful:


var signalRConnection = new HubConnectionBuilder()
     .WithUrl(BaseUrl + "/hubs/integrationserver", options =>
     {
      options.Headers.Add("Authorization", "Basic " + Header);
     }).Build();
signalRConnection.On<string, string>("ReceiveMessage", (entityName, json) =>
{

});


private async void OpenSignalRConnection()
{
var pauseBetweenFailures = TimeSpan.FromSeconds(20);

var retryPolicy = Policy .Handle<Exception>() .WaitAndRetryForeverAsync(i => pauseBetweenFailures , (exception, timeSpan) => { Log.Error(exception.ToString()); }); await retryPolicy.ExecuteAsync(async () => { Log.Information("Trying to connect to SignalR server"); await TryOpenSignalRConnection(); }); }

private async Task<bool> TryOpenSignalRConnection()
{

Log.Information("Starting SignalR connection");

await signalRConnection.StartAsync();
Log.Information("SignalR connection established");
return true;
}


private async Task SignalRConnection_Closed(Exception arg)
{
Log.Information("SignalR connection is closed");

await signalRConnection.StopAsync();

signalRConnection.Closed -= SignalRConnection_Closed;
OpenSignalRConnection();
}

6 Comments on .NET Core SignalR Automatic Reconnects

  1. cristi
    August 23, 2018 at 12:56 pm (6 years ago)

    Looks very good and helpful!

    Reply
  2. Cleyton Bruno
    December 30, 2018 at 3:12 pm (5 years ago)

    Hi,

    I can see Polly will forever call TryOpenSignalRConnection until the connection is open successfully. In this method, I see you’re subscribing to the closed event:

    signalRConnection.Closed += SignalRConnection_Closed;

    Wouldn’t this cause the subscription to be made multiple times, ending in something like this https://dotnetfiddle.net/iW4eAN?

    Reply
    • Max Markelow
      January 11, 2019 at 8:39 am (5 years ago)

      Yes, it would be. It’s necessary to unsubscribe in WaitAndRetryForeverAsync lyambda.

      Reply
      • radenko
        February 12, 2019 at 7:33 am (5 years ago)

        Yes you are right. I corrected that line of code.

        Reply
        • Richard M
          April 9, 2019 at 1:20 pm (5 years ago)

          Looks like you removed the line for adding the event listener:
          signalRConnection.Closed += SignalRConnection_Closed;

          Reply
  3. Vlad
    April 10, 2019 at 11:45 am (5 years ago)

    Hello. I have a question. Where I can find your implementation because I
    not quite figured out how to apply this is code.

    Reply

Leave a Reply