如何取消订阅服务器端的观察者

时间:2013-01-09 08:36:10

标签: system.reactive .net-remoting

我有一个应用程序使用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
}

1 个答案:

答案 0 :(得分:0)

我认为你要做的就是吞下异常并让你的服务器继续运行。您可以通过捕获异常(就像您在此处所做的那样)来执行此操作,然后可以将其传递给相关Observable的OnError方法,以便订阅者可以选择如何做出反应。