How to properly handle a failed WCF connection?

advertisements

In my client program, there is a WCF connection that is opened at startup and supposedly stays connected til shutdown. However, there is a chance that the server closes due to unforeseeable circumstances (imagine someone pulling the cable).

Since the client uses a lot of contract methods in a lot of places, I don't want to add a try/catch on every method call.

I've got 2 ideas for handling this issue:

  1. Create a method that takes a delegate and executes the delegate inside a try/catch and returns an Exception in case of a known exception, or null else. The caller has to deal with nun-null results.

  2. Listen to the Faulted event of the underlying CommunicationObject. But I don't see how I could handle the event except for displaying some error message and shutting down.

Are there some best practices for faulted WCF connection that exist for app lifetime?


I wrote a blog post on exceptions in WCF that deals with how to handle this: http://jamescbender.com/bendersblog/Default.aspx