【问题标题】:WCF how detect client existencyWCF如何检测客户端存在
【发布时间】:2010-05-19 13:12:20
【问题描述】:

正如我的标题中所写,

背景: 我有 2 种不同类型的应用程序 (WPF-silverlight) 可以相互通信 - 医生应用程序和患者应用程序 - 但这并不意味着只有 2 个应用程序会运行,例如: 我可以运行 3 个医生应用程序和 7 个患者应用程序。 并且所有这些应用程序都通过 tcp 连接使用 wcf 进行通信。 通信是实时的(如信使应用程序)

流动 每次有应用程序在线(运行)时,我都会在 wcf 注册它的连接,因为我需要让其他应用程序知道(实时)有新客户端连接或新客户端断开连接。

问题: 可以让其他应用知道有传入的应用/客户端, 但我的问题是,如何让其他应用知道此客户端是否已断开连接,

如果用户正确关闭应用程序(例如单击关闭按钮)很好 - 所以在 wpf 中,我可以调用 wcf 取消注册连接,

但是如果连接异常终止(例如,直接拔掉你电脑的电源线)怎么办? 有什么方法可以让我知道这个客户端是否仍然连接?

当我在我的 VS2008 中按 f5 并关闭,然后再次打开并关闭(重复)然后,当我调试时,仍然有很多连接存储在那里,但实际上客户端已经被破坏,我意识到了这个问题。

所以有人知道这个最佳解决方案吗? 非常感谢示例

我的代码 sn-p:

Dictionary<Guid, Client> Connections = new Dictionary<Guid, Client>();
// above is the variable where i put the connections
    object syncObj = new object();

    public ITcpServiceCallback CurrentCallback { get { return OperationContext.Current.GetCallbackChannel<ITcpServiceCallback>(); } }

    // this function is called when the program started
    public List<Client> ShakeHand( Client client, RoleType appType ) {
        if( GetClientsByCallback( CurrentCallback ).Count < 1 && GetClientsByID( client.ID ).Count < 1 ) {
            List<Client> retVal = new List<Client>();
            lock( syncObj ) {
                if( appType == RoleType.Doctor ) {
                    List<Client> doctors = Helpers.GetDoctor( AppDomain.CurrentDomain.BaseDirectory + "App_Data/doctor.xml" );
                    foreach( Client doctor in doctors ) {
                        doctor.Status = ConnectionStatus.Offline;
                        foreach( Client con in Connections.Values ) {
                            if( con.Role == RoleType.Doctor && con.ID == doctor.ID ) {
                                doctor.Status = ConnectionStatus.Online;
                                break;
                            }
                        }
                        retVal.Add( doctor );
                    }
                } else { //b la.. bla similiar like if above
                }

                client.Callback = CurrentCallback;
                client.Status = ConnectionStatus.Online;

                // this is the code where i add the connection
                Connections.Add( Guid.NewGuid(), client ); 

            }
            return retVal;
        }
        return null;
    }

提前谢谢你

【问题讨论】:

    标签: wpf wcf service tcp


    【解决方案1】:

    我认为您真正需要实现的是发布者/订阅者服务。所有医生和患者的应用程序都有一个指向 pub/sub 服务的链接。当一个应用程序启动时,它会订阅该服务并开始接收来自所有其他应用程序的通知。当应用程序关闭时,它会取消订阅事件。

    看看这个: http://msdn.microsoft.com/en-us/library/ms752254.aspx

    还有这个: http://msdn.microsoft.com/en-us/magazine/cc163537.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-28
      相关资源
      最近更新 更多