Say that I got a WCF client that communicates with a WCF service in IIS7(TCP) using channelfactory. If the communication is broken(or the service recycles) then the next call from the client will result in an exception.
At this point I need to hide the exception and instead try to make a reconnect/re-login.
The question is if there is any simple way to do this without adding handling code in every ServiceAgentMethod on the client side? Is this for example possible to solve with a MessageInspector?
In my case I got several Agent classes in the client that runs the service methods with a singular IServceInterface created from ChannelFactory. Something like this :
MyServiceAgent.GetService().GetMyObjects
The GetService() will return the IServiceInterface that is created on login by the ChannelFactory.
I wouldn’t try it using a message inspector; when a channel faults, it faults hard (as you’ve seen, you get an exception). It’s used to indicate that the channel is no longer considered valid and that you shouldn’t make calls on it.
You might think that you could do this at the transport layer, but you’d need coordination of the layers above it (in order to translate what comes in over the wire, which you really shouldn’t have access to) in order to determine whether or not you have a fault.
Ultimately, you would want to issue retry logic at the level where you make the call to the proxy. Have an abstraction of the client (an interface) which you intercept before/after you make the call to the proxy; if there’s an exception, you can retry the call, creating a new proxy/channel as needed.
Be careful to put a limit on the number of retries you perform; if you don’t you’ll end up with a poison pill (your code will retry infinitely). At some point, you’ll want to throw the exception.