Alex Rivera | Logout

How to handle WCF exceptions (consolidated list with code)

Asked 2011-05-25T20:14:30.743
49

I'm attempting to extend this answer on SO to make a WCF client retry on transient network failures and handle other situations that require a retry such as authentication expiration.

Question:

What are the WCF exceptions that need to be handled, and what is the correct way to handle them?

Here are a few sample techniques that I'm hoping to see instead of or in addition to proxy.abort():

  • Delay X seconds prior to retry
  • Close and recreate a New() WCF client. Dispose the old one.
  • Don't retry and rethrow this error
  • Retry N times, then throw

Since it's unlikely one person knows all the exceptions or ways to resolve them, do share what you know. I'll aggregate the answers and approaches in the code sample below.

    // USAGE SAMPLE
    //int newOrderId = 0; // need a value for definite assignment
    //Service<IOrderService>.Use(orderService=>
    //{
    //  newOrderId = orderService.PlaceOrder(request);
    //}




    /// <summary>
    /// A safe WCF Proxy suitable when sessionmode=false
    /// </summary>
    /// <param name="codeBlock"></param>
    public static void Use(UseServiceDelegateVoid<T> codeBlock)
    {
        IClientChannel proxy = (IClientChannel)_channelFactory.CreateChannel();
        bool success = false;
        try
        {
            codeBlock((T)proxy);
            proxy.Close();
            success = true;
        }
        catch (CommunicationObjectAbortedException e)
        {
                // Object should be discarded if this is reached.  
                // Debugging discovered the following exception here:
                // "Connection
Edit
Report

1 Answer

4

I started a project on Codeplex that has the following features

  • Allows efficient reuse of the client proxy
  • Cleans up all resources, including EventHandlers
  • Operates on Duplex channels
  • Operates on Per-call services
  • Supports config constructor, or by factory

http://smartwcfclient.codeplex.com/

It is a work in progress, and is very heavily commented. I'll appreciate any feedback regarding improving it.

Sample usage when in instance mode:

 var reusableSW = new LC.Utils.WCF.ServiceWrapper<IProcessDataDuplex>(channelFactory);

 reusableSW.Reuse(client =>
                      {
                          client.CheckIn(count.ToString());
                      });


 reusableSW.Dispose();
answered 2011-06-01T00:55:46.400

Your Answer