4

I am using latest SignalR version 2.3.0 with C# client. The problem I am facing is kind of mentioned here on stackoverflow. But that issue is few years old and it is already closed on github.

The issue I am facing is that whenever a request timeout from client to server then client starts reconnecting to server and then keeps on reconnecting. I tried defining the WebSocket protocol also in connection but result is still same.

 _connection.Start(new Transports.WebSocketTransport()).Wait(TimeSpan.FromSeconds(10));

I am looking for a persistent connection between server and client.

I have added an event handler on StateChanged event to capture the state changes of SignalR client.

Here's the screenshot of reconnecting state

enter image description here

Does anybody have any idea why it's happening and how it can be prevented?

prem
  • 3,348
  • 1
  • 25
  • 57

1 Answers1

2

The reason might be because your OnReconnect even doesn't fire and then again your OnDisconnected doesn't fire when the client calls the `stop method as described here:

If the client goes into reconnecting mode but can't establish a transport connection within the disconnect timeout limit, the server terminates the SignalR connection. When that happens, the server executes the Hub's OnDisconnected method and queues up a disconnect message to send to the client in case the client manages to connect later. If the client then does reconnect, it receives the disconnect command and calls the Stop method. In this scenario, OnReconnected is not executed when the client reconnects, and OnDisconnected is not executed when the client calls Stop.

This is referring to SignalR version 2, But I believe that it will of help to you as well.

Worth mention as well, that you will contentiously try to reconnect if you call start from your closed event (or disconnect in JavaScript`) as said:

In some applications you might want to automatically re-establish a connection after it has been lost and the attempt to reconnect has timed out. To do that, you can call the Start method from your Closed event handler (disconnected event handler on JavaScript clients). You might want to wait a period of time before calling Start in order to avoid doing this too frequently when the server or the physical connection are unavailable. The following code sample is for a JavaScript client using the generated proxy.

anyway, the simplest way to disconnect your client from the server up until version 2 is to implement the disconnect method on the client an call it from the server as mentioned:

SignalR version 2 does not have a built-in server API for disconnecting clients. There are plans for adding this functionality in the future. In the current SignalR release, the simplest way to disconnect a client from the server is to implement a disconnect method on the client and call that method from the server.

Barr J
  • 10,636
  • 1
  • 28
  • 46
  • 1
    Thanks for your reply. You have pointed me to the right direction. I have checked the OnReconnected() event and found that I have written some code before base.OnReconnected() event which is causing exception. The code lines causing issue is HttpContext.Current.Request.ServerVariables["HTTP_HOST"]; This is working fine OnConnected() and OnReconnected normally but throwing null reference on reconnection after request timeout. Any idea about this? – prem Aug 20 '18 at 09:11
  • try calling the start method from your closed event handler as mentioned in the article, it might do the magic. – Barr J Aug 20 '18 at 09:40
  • 1
    The problem is on Reconnecting event before closed. Now when I know the issue I can use workaround and pass the server url in header instead of using HttpContext.Current.Request.ServerVariables["HTTP_HOST"]; Thanks for your help and will definitely mark it as answer after finishing my implementation. – prem Aug 20 '18 at 09:56