tags:

views:

49

answers:

1

Greetings, in our company we are developing wcf service. This is used as a server and it works quite well. Hover there is a wish from customer that after they login to application they would like to see which users are logged in too. I read about CallbackContract (based on some wcf chat application). How can we achive this goal?

A: 

Similar question asked here

You can deffinetly manage the logged users inside the server. I have created a personal pattern for dealing with such situations, and it ussually goes like this:

  • create a client class inside the WCF server that will hold all the needed information about the client.

  • create 2 methods in the service: logIn, logOut. the login method should be able to gather all the informations about the client that you want to store. Make sure to define properties that can uniquely identify a client instance. When the client conencts to the server it calls the login method, allowing the server to gather and save the information from the client. If using callbacks, this is the place to save the CallBack context object, in the client obejt. You can now save the Client object in the WCF server instance (I use a dictioary). When the client logs out, it calls the log out method and the server removes the entry.

  • create a KeepAlive method in the server that regularry checks the connected clients to see if they are still connected (in case of network failure or app crash a client may not call the logout method).

I think this is the simplest way (not saying it's the best) to manage clients in the server. There is no problem with having multiple clients from the same computer (you save the Context when a client logges in) as long as you have a way of uniquely identify clients.

As for your last question, having multiple services should not be a problem. In fact you have the same WCF server with different contracts (and endpoints) for the different services you offer. ALl the contracts reside in the same WCF server instance so they all can access the connected client list.

If you have further questions, I would be happy to answer them.

You can find the code you need to actually build the WCF service you require here

Neil
wow..thank you Neil it helps me a lot. Just one more question. Can you please provide me some advices about KeepAlive method which will regularry check the connected clients?
niao
Basically its a ping, the server calls a method on the client called public void ping(), and if the call results in a CommuicationException then it means the client cannot respond and should be removed from the list of online clients. this method should be placed in a Timer and set to cycle through all the clients every x mins
Neil
any example how can I do this?
niao
I cant seem to find an article describing this process, but you will have to write a singleton WCF service that does the pinging, this singleton class stores a List of users that are currently logged in and cycles through (using a Timer) the List and calls the ping method if it fails then it removes the user from the list
Neil