Notify SignalR IIS restart / shutdown clients

I have a SignalR server (web app) and a client (console app).

I want my Client to be notified when my server's IIS reboots / shuts down, or the server just restarts / shuts down.

Is it possible?

+3


source to share


2 answers


You can do it like this:

var connection = new HubConnection(hubUrl);
if (configureConnection != null)
    configureConnection(connection);

var proxy = connection.CreateHubProxy("EventAggregatorProxyHub");
connection.Reconnected += reconnected;

      

https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy/blob/master/SignalR.EventAggregatorProxy.Client.DotNet/Bootstrap/Factories/HubProxyFactory.cs#L17

Other events

  • Reconnect - Will be triggered when it tries to reconnect after closing the connection.
  • Closed - Fired when connection is lost


Update:

Closed

will be called if re-creation fails (when IIS has been disconnected for a longer period than accepted by the reconnect timeout).

This means that you have to reconnect to the Close event using connection.Start()

when it fails. The closed event will be called again and try again with connection.Start()

.

Here is an example of using my code, it will stand both that IIS does not work when the application is started, and that it works when it runs

public class HubProxyFactory : IHubProxyFactory
{
    public IHubProxy Create(string hubUrl, Action<IHubConnection> configureConnection, Action<IHubProxy> onStarted, Action reconnected, Action<Exception> faulted, Action connected)
    {
        var connection = new HubConnection(hubUrl);
        if (configureConnection != null)
            configureConnection(connection);

        var proxy = connection.CreateHubProxy("EventAggregatorProxyHub");
        connection.Reconnected += reconnected;
        connection.Error += faulted;

        var isConnected = false;

        Action start = () =>
        {
            Task.Factory.StartNew(() =>
            {
                try
                {
                    connection.Start().Wait();
                    if(isConnected)
                        reconnected();
                    else
                    {
                        isConnected = true;
                        onStarted(proxy);
                        connected();
                    }
                }
                catch(Exception ex)
                {
                    faulted(ex);
                }
            });
        };

        connection.Closed += start;

        start();

        return proxy;
    }
}

      

+4


source


The only solution I have right now (mostly Indian Jubaba) is to try to reconnect to the SignalR server every time for a certain amount of time.

HubConnection _connection = new HubConnection(Properties.Settings.Default.UserLoginDataSource);
IHubProxy _hub;

void TryConnectingDataSource()
{
 try
 {

   _connection.Stop();
   _connection.Dispose();

   _connection = new HubConnection(Properties.Settings.Default.UserLoginDataSource);
   _connection.StateChanged += connection_StateChanged;
   _hub = _connection.CreateHubProxy("myHub");
   _connection.Start().Wait();

   if (_connection.State == ConnectionState.Connected)
   {
      _hub.On("myHubCallback", new Action<Dictionary<string, Entities.Permissions.UserTypes>>(GetUserLoginList));
      _hub.Invoke("myHubMethod");
   }
 }
 catch (Exception x)
 {
   EventLogger.LogError(x);
 }
}

      



If I catch Catch, it means that there is something wrong with Connection, most likely ISS is not working.

0


source







All Articles