I am creating a client service application using WCF duplex callback. The service sends messages to all connected clients. I am storing all connected clients in the dictionary. Before sending message to the client I want to check if any client has shutdown abnormally (network disconnection, power off).
@marc_s: I created a CheckCallbackChannels method which will check for Communication State of a connected clients in the dictionary.
Sub CheckCallbackChannel()
For Each objClient As KeyValuePair(Of Guid, IClientCallBack) In MainService.objClients
Dim objClientCallBack As IClientCallBack = MainService.objClients.Item(objClient.Key)
Dim callbackChannel As ICommunicationObject = TryCast(mobjClientCallBack,ICommunicationObject)
'Dim state As CommunicationState = callbackChannel.State
If callbackChannel.State = CommunicationState.Opened OrElse callbackChannel.State = CommunicationState.Faulted Then
MainService.objClients.Remove(objClient.Key)
End If
Next
End Sub
but here i get the state as connected for faulty clients also.
Yes, you could include a Ping()
method on each service contract, and call that before your actual call.
But what does that really tell you??
First of all, even the call to .Ping()
could result in an exception (since the service is gone, overloaded, whatever).
And if it succeeds - it gives you absolutely no guarantee that your next call to that same service, a microsecond later, has any chance of succeeding....
Basically, I find this approach wasteful - you need to write a lot of code, and in the end, it really doesn't give you any meaningful information.
What you need to do is: be prepared on every service call that it can fail - for whatever reason. Don't waste your time and processing cycles pinging around all over your system - it doesn't give you any meaningful info anyway.
Just make your service calls, be prepared to handle failures (and possibly recycle your WCF proxies) - and that's all you can really do.