tags:

views:

1213

answers:

4

How can I handle WCF Client Disconnects ?

I Tried CallBacks but it seems to only works when Client Disconnect "clearly" by using a "Disconnect" button for example.

I want my server to be notified when Client got disconnected, even on manual disconnect or client process crash.

A: 

Why not have a loop where the client just pings the service every second or so and then do a callback. when the callback fails then the client has disconnected.

Jonathan Parker
A: 

A variation on polling would be to use messaging. The client could post an "I'm here" message and then delete the message when it disconnects. The server could monitor the queue for disconnects. Another option is to use BOSH, bi-directional HTTP communication. This is what web chat clients use.

Adam Fyles
+2  A: 

Use the OperationContext.Current.Channel.Faulted and OperationContext.Current.Channel.Closed events.

These events are fired when the client has disconnected for whatever reason.

Matt Norrie
A: 

I would say that if the client disconnects in a abnormal way there´s no way to make sure that the server will be aware of that. It´s pretty much like HTTP. If we close the browser, let´s say with a force quit, the browser won´t send anything to the server.

That´s one of many reasons why we have session timeout. To disconnect a client and clean up in the server side everything that is being used if a client do something wrong (too long operations for example) or disconnects without letting the server know.

tucaz