Dealing with WCF service restart on client-side

theburningmonk picture theburningmonk · Dec 31, 2009 · Viewed 12.3k times · Source

I've got a GUI client which is running against a WCF services hosted as a Windows service on a server box. The WCF service is running in PerCall InstanceContextMode, and the client has a singleton instance of the service client and I want to avoid reinstantiating the singleton on every call as it makes life difficult for the many asynchronous calls I have.

The problem for me is, after the Windows service is restarted, everytime the client makes a call it gets an exception message like this:

This channel can no longer be used to send messages as the output session was auto-closed due to a server-initiated shutdown. Either disable auto-close by setting the DispatchRuntime.AutomaticInputSessionShutdown to false, or consider modifying the shutdown protocol with the remote server.

What's the best way to get around this? I can put try-catch clauses around all the calls to the service client and reinstantiate the singleton instance on communication exceptions but that will involve a lot of boilerplate code..

Answer

marc_s picture marc_s · Dec 31, 2009

The best would be to avoid the exceptions on the server all together. If a WCF Server encounters an exception that is not being caught and handled, it will "fault" the channel, rendering it useless.

On the server side, you can implement the IErrorHandler interface and catch .NET exceptions, turning them into SOAP faults which will be handed back to the client more gracefully, without faulting the channel.

That way, you can catch all .NET exceptions on the server, and convert them into interoperable SOAP faults which will not cause these problems.

For more information, see: