2010-05-19 161 views
2

正如我标题writen,WCF如何检测客户端existency

背景: 我有2个不同类型的应用程序(WPF,Silverlight的),它可以跟对方 - 医生应用程序和患者应用程序 - 但事实并非如此意味着只有2个应用程序可以运行,例如: 我可以运行3个医生应用程序和7个患者应用程序。 和所有这些应用程序是通过TCP连接使用wcf进行通信。 通信是实时的(如Messenger应用)

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

问题: 它的罚款,让其他应用程序知道有入局应用程序/客户端, 但我的问题是,如何让其他应用程序知道这个客户端断开连接,

这很好,如果用户关闭应用程序(例如,单击关闭按钮) - 所以在wpf中,我可以调用wcf来取消注册连接,但是如果连接终止异常(例如,直接拔下PC的电源电缆) 有没有什么办法让我知道这个客户端仍然连接?

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

有人知道这个最好的解决方案吗? 例子是非常赞赏

我的代码片段:

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

预先感谢您

回答