WCF how to determine if a client exists
As my name says,
background: I have two different types of apps (WPF-silverlight) that can talk to each other - Doctor app and Patient app, but that doesn't mean only 2 apps will run, for example: I can run the app 3 doctors and 7 patients. and all these applications communicate with wcf over tcp connection. real-time communication (e.g. messaging app)
thread every time there is an application online (launch) I log its connection to wcf because I need other applications to know (in real time) that a new client is connected or a new client is disconnected.
problem: it's good for another app to know that there is an incoming app / client, but my problem is how to tell another app that this client has been disconnected,
it's ok if the user closes the app correctly (for example, click the close button) - so in wpf I can call wcf to unregister the connection,
but how if the connection was interrupted abnormally (for example, directly unplug the power cable from the PC) is there a way to know if this client is still connected or not?
I implement this problem when I pres f5 in my VS2008 and close and reopen and close (repeat) and then when I debug there are still a lot of connections there, but in fact the client is already destroyed.
so anyone know this best solution? example is much appreciated
my code snippet:
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;
}
Thanks in Advance
a source to share
I think you really need to implement a publisher / subscriber service. All applications for doctors and patients have a pub / sub service link. When the app is launched, it subscribes to the service and starts receiving notifications from all other apps. When the app exits, it doesn't subscribe to events.
Take a look at this: http://msdn.microsoft.com/en-us/library/ms752254.aspx
and this: http://msdn.microsoft.com/en-us/magazine/cc163537.aspx
a source to share