2013-01-09 61 views
1

我有一個應用程序使用RX通過遠程處理一臺服務器(可觀察)和許多客戶端(觀察員)。 我的問題是當一個客戶端(觀察者)不正確地斷開連接而沒有執行ubsubscribe(dispose)時,服務器上的OnNext()函數開始拋出遠程異常。如何取消訂閱服務器端的觀察者

是否有任何機制來解決服務器端有問題的觀察者?

的客戶端代碼的一部分:

internal void SetRemoting(bool refreshInstance) 
{ 
    string channelName = "RemotingClientUI"; 
    IDictionary dict = new Hashtable(); 
    dict["port"] = 9988; 
    dict["name"] = channelName; 

    var bcp = new BinaryClientFormatterSinkProvider(); 
    var channel = new TcpClientChannel(dict, bcp); 
    ChannelServices.RegisterChannel(channel, false); 

    _remoteServer = (IRemoteServerService) Activator. 
     GetObject(typeof (IRemoteServerService), 
      tcp://..."); 
} 

private void SubscribeToRemoteEvents(bool unSubscrubeFirst) 
{    
    _jobRowUpdate = _remoteServer.JobRowUpdate.Subscribe(UpdateJobQueueRow); 
    _packageRowUpdate = _remoteServer.PackageRowUpdate. 
     Subscribe(UpdatePackageQueueRow); 
    _miscUpdate = _remoteServer.MiscAction.Subscribe(MiscRemoteActions); 
} 

的服務器代碼的部分:

public class RemoteServiceService 
{ 
    public RemoteServiceService() 
    { 
     JobRowUpdate = LoggerFactory.GetLogger(
      LoggerType.RemoteService, this).JobRowUpdate.Remotable(); 
     PackageRowUpdate = LoggerFactory.GetLogger(
      LoggerType.RemoteService, this).PackageRowUpdate.Remotable(); 
     MiscAction = LoggerFactory.GetLogger(
      LoggerType.RemoteService, this).MiscActions.Remotable(); 
    } 
} 

public class RemoteLoggerForService 
{ 
    private RemoteLoggerForService(IService service) 
    { 
     _jobRowUpdate = new Subject<IJobQueueRow>(); 
     _packageRowUpdate = new Subject<IPackageQueueRow>(); 
     _miscActions = new Subject<MiscRemoteObjects>(); 
     _service = service; 
    } 

    #region Overrides of LoggerBase 

    public override void WriteToLog<T>(T stringFormatOrObject, 
     params object[] args) 
    { 
     lock (this) 
     try 
     { 
      lock (LockLogger) 
      { 
       if (stringFormatOrObject is IJobQueueRow && 
        _jobRowUpdate != null) 
       { 
        _jobRowUpdate.OnNext(
         stringFormatOrObject as IJobQueueRow); 
       } 

       if (stringFormatOrObject is IPackageQueueRow && 
        _packageRowUpdate != null) 
       { 
        _packageRowUpdate.OnNext(
         stringFormatOrObject as IPackageQueueRow); 
       } 

       if (stringFormatOrObject is MiscRemoteObjects && 
        _miscActions != null) 
       { 
        _miscActions.OnNext(
         stringFormatOrObject as MiscRemoteObjects); 
       } 
      } 
     } 
     catch(Exception ex) 
     { 
      LoggerFactory.GetLogger(LoggerType.File, null). 
       WriteToLog(
        Utils.GetFullException("RemoteLoggerForService", ex)); 
     } 
    } 

    #endregion 
} 
+0

考慮重新措辭這一點。我不明白你的問題。當問題出現在一個或另一個問題上時,您似乎可能會將Rx和遠程問題混爲一談。 –

+0

可觀察者和觀察者在不同的計算機上。我正在使用ISubject .Remotable()擴展名。一切工作正常,但斷開客戶端導致服務器粉碎與「遠程異常」。問題是如何導致Observable集合忽略或刪除已經斷開連接的客戶端的訂閱 – user1960639

+0

你如何訂閱?您是否添加了錯誤/異常處理程序?或一個抓住?您需要提供更多信息。 –

回答

0

我認爲你正在試圖做的是吞嚥異常,並在您的服務器進行。您可以通過捕獲異常(就像您在此完成的那樣)來完成此操作,然後您可以將其傳遞給相關Observable的OnError方法,以便用戶可以選擇如何作出反應。

相關問題