locked
C# Best Practices for Service Bus Clients Exceptions / Retry for sporadic network issues. RRS feed

  • Question

  • I'm using the following class to handle exceptions and to issue retries to my WCF-based server. What changes will I need to make to make this compatible with the service bus (given it's way of handling exceptions)

    Sample WCF Client Usage

    Service.Use(orderService=> { orderService.PlaceOrder(request); }

    Service Delegate.cs

    This is the file I use to handle any unique situations WCF may send my way. What changes are needed to support the Service Bus... please edit as needed.

    public static class Service<T>
       
    {

          
    public delegate void UseServiceDelegate<T>(T proxy);
           
    public static ChannelFactory<T> _channelFactory = new ChannelFactory<T>("");

           
    public static void Use(UseServiceDelegate<T> codeBlock)
           
    {
               
    IClientChannel proxy = (IClientChannel)_channelFactory.CreateChannel();
               
    bool success = false;



              
    Exception mostRecentEx = null;
              
    for(int i=0; i<5; i++)  // Attempt a maximum of 5 times
              
    {
                  
    try
                  
    {
                       codeBlock
    ((T)proxy);
                       proxy
    .Close();
                       success
    = true;
                  
    }


                  
    // The following is typically thrown on the client when a channel is terminated due to the server closing the connection.
                  
    catch (ChannelTerminatedException cte)
                  
    {
                      mostRecentEx
    = cte;
                       proxy
    .Abort();
                      
    //  delay (backoff) and retry
                      
    Thread.Sleep(1000 * (i + 1));
                  
    }

                  
    // The following is thrown when a remote endpoint could not be found or reached.  The endpoint may not be found or
                  
    // reachable because the remote endpoint is down, the remote endpoint is unreachable, or because the remote network is unreachable.
                  
    catch (EndpointNotFoundException enfe)
                  
    {
                      mostRecentEx
    = enfe;
                       proxy
    .Abort();
                      
    //  delay (backoff) and retry
                      
    Thread.Sleep(1000 * (i + 1));
                  
    }

                  
    // The following exception that is thrown when a server is too busy to accept a message.
                  
    catch (ServerTooBusyException stbe)
                  
    {
                      mostRecentEx
    = stbe;
                       proxy
    .Abort();


                      
    //  delay (backoff) and retry
                      
    Thread.Sleep(1000 * (i + 1));
                  
    }

                  
    catch(Exception ex)
                  
    {
                       
    // rethrow any other exception not defined here
                       
    // You may want to define a custom Exception class to pass information such as failure count, and failure type
                        proxy
    .Abort();
                       
    throw ex; 
                  
    }
              
    }
              
    if (mostRecentEx != null)
              
    {
                   proxy
    .Abort();
                  
    throw new Exception("WCF call failed after 5 retries.", mostRecentEx );
             }

       }

    }





    • Edited by ChrisLaMont Tuesday, February 21, 2012 10:46 PM
    Tuesday, February 21, 2012 10:09 PM

Answers

  • Hi,

    I can not count any extra exceptions if you want to expose WCF service to Service Bus, if you have further issues, please post it on this thread.


    • Edited by New GYW Wednesday, February 22, 2012 8:19 AM
    • Marked as answer by Arwind - MSFT Monday, February 27, 2012 8:57 AM
    Wednesday, February 22, 2012 8:18 AM