2015-05-19 34 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關閉。