2015-05-19 35 views
3

我有一个SignalR服务器(一个Web应用程序)和一个客户端(控制台应用程序)。通知SignalR IIS重新启动/关闭客户端

我希望我的客户端在我的服务器的IIS重启/关闭或服务器重启/关闭时得到通知。

可能吗?

+0

你的意思是重新启动整个IIS服务器或只是你的应用程序的工作进程? –

+0

作为计划任务的触发器,或[Windows服务](http://forums.iis.net/t/1180869.aspx?IIS+Windows+Service)? – lloyd

+0

@AliHasan,我相信两者都是一样的,但我更关注工作流程的重启。 – yogi

回答

4

你可以像下面这样做

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

其他事件

  • 重新连接 - 当它试图重新连接关闭后激发
  • 封闭 - 被解雇时连接丢失

更新:在重新连接失败

Closed将被调用(当IIS已连续比重新连接超时接受更长的时间)。

这意味着您应该使用connection.Start()在关闭事件失败时重新连接,关闭事件将再次被调用,并且可以再次使用connection.Start()重试。

下面是一个使用我的代码的一个例子,它才能生存,这两个IIS正在下降时,应用程序启动和它的股价下跌,同时运行

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; 
    } 
} 
+0

我相信重新连接只会在重新启动后/如果启动后启动。我想通知它什么时候发生。没有提到有问题抱歉。将更新。 – yogi

+0

查看更新请 – Anders

+0

封闭似乎很有希望,可能会工作。谢谢,我会尝试一下。将接受,如果这样的作品:) – yogi

0

我截至目前唯一的解决办法(非常印度jubaad的)是每次在特定的时间间隔内尝试重新连接到SignalR服务器。

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); 
} 
} 

如果我会被Catch抓住,那将意味着Connection出现问题,最有可能的是ISS关闭。