Submit to Reddit      
  

ChannelAdam WCF Library — Version 2 Documentation

Retry For Transient Fault Handling

Overview

When you call your web services, do you need to handle transient errors that may occur (such as the network briefly dropping out), and have some automatic retry logic?

The ChannelAdam WCF Library has built-in support for plugging in a retry policy.

** Please note: unlike version 1, retry policies in version 2, when set, are applied to service calls via both the Operations property and also the Consume method.**

Default Retry Policy

Out of the box, no retry policy is applied to service calls. You must specifically set a retry policy for one to be applied.

The Retry Policy Mechanism

A retry policy is implemented as an instance of a class that implements the interface ChannelAdam.TransientFaultHandling.IRetryPolicyFunction (from the ChannelAdam Core NuGet Library). The IRetryPolicyFunction interface contains one simple Execute function, allowing any retry mechanism to be plugged into the ServiceConsumerFactory or ServiceConsumer.

Support For The Microsoft Practices TransientFaultHandling.Core Library

The additional NuGet library ChannelAdam WCF Library Extensions for the Microsoft Practices TransientFaultHandling.Core Library provides extensions to the ChannelAdam WCF Library to support the usage of the Microsoft Practices Transient Fault Handling Library.

How To Set A Retry Policy

There are three ways to set a retry policy:

  1. Setting the static property DefaultRetryPolicy on the ServiceConsumerFactory class - which will then apply to all new created instances of ServiceConsumer;
  2. Specifying the retry policy in one of the Create() method overloads on the ServiceConsumerFactory; or
  3. By setting the RetryPolicy property directly on an instance of ServiceConsumer itself.

You could easily set the static property on the ServiceConsumerFactory in the bootstrap code for your application, and it will apply to all ServiceConsumer service operation calls thereafter. Easy!

Example Usage Of A Retry Policy

This sample code uses the Microsoft Practices Transient Fault Handling Library, supported by the ChannelAdam WCF Library Extensions for the Microsoft Practices TransientFaultHandling.Core Library.

You will notice in this example, I am using the ChannelAdam.ServiceModel.SoapFaultWebServiceTransientErrorDetectionStrategy (from the ChannelAdam.Wcf.Microsoft.Practices.TransientFaultHandling.Core NuGet Library) to determine if there was a transient error or not. If the given exception was a FaultException, this strategy says that was not a transient error and therefore do not retry. If on the other hand any other type of exception occurs, then this strategy will tell the Microsoft library to retry according to the retry policy.

The following sample code requires the ChannelAdam.Wcf.Microsoft.Practices.TransientFaultHandling.Core NuGet Library to be installed.

1. Sample Retry Code Using the Operations Property

using ChannelAdam.ServiceModel;
using Microsoft.Practices.TransientFaultHandling;

...

var microsoftRetryStrategy = new Incremental(5, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2));
var microsoftRetryPolicy = new RetryPolicy<SoapFaultWebServiceTransientErrorDetectionStrategy>(microsoftRetryStrategy);

ServiceConsumerFactory.DefaultRetryPolicy = microsoftRetryPolicy.ForServiceConsumer(); // extension method
// or ServiceConsumerFactory.DefaultRetryPolicy = new ChannelAdam.TransientFaultHandling.RetryPolicyAdapter(microsoftRetryPolicy);
// or ServiceConsumerFactory.DefaultRetryPolicy = (ChannelAdam.TransientFaultHandling.IRetryPolicyFunction)microsoftRetryPolicy;
// Because DefaultRetryPolicy has been set, the retry policy will be applied when AddIntegers() is called.

using (var service = ServiceConsumerFactory.Create<IFakeService>(() => new FakeServiceClient()))
{
    try
    {
        int actual = service.Operations.AddIntegers(1, 1);
    }
    catch (FaultException fe)
    {
        Console.WriteLine("Service operation threw a fault: " + fe.ToString());
    }
    catch (Exception ex)
    {
        Console.WriteLine("Technical error occurred while calling the service operation: " + ex.ToString());
    }
}

2. Sample Retry Code Using the Consume Method

The retry policy works exactly the same when the Consume() method is used.

using ChannelAdam.ServiceModel;
using Microsoft.Practices.TransientFaultHandling;

...

var microsoftRetryStrategy = new Incremental(5, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2));
var microsoftRetryPolicy = new RetryPolicy<SoapFaultWebServiceTransientErrorDetectionStrategy>(microsoftRetryStrategy);

ServiceConsumerFactory.DefaultRetryPolicy = microsoftRetryPolicy.ForServiceConsumer(); // extension method
// or ServiceConsumerFactory.DefaultRetryPolicy = new ChannelAdam.TransientFaultHandling.RetryPolicyAdapter(microsoftRetryPolicy);
// or ServiceConsumerFactory.DefaultRetryPolicy = (ChannelAdam.TransientFaultHandling.IRetryPolicyFunction)microsoftRetryPolicy;
// Because DefaultRetryPolicy has been set, the retry policy will be applied when AddIntegers() is called.

using (var service = ServiceConsumerFactory.Create<IFakeService>(() => new FakeServiceClient()))
{
    var result = service.Consume(operation => operation.AddIntegers(1, 1));

    if (result.HasNoException)
    {
        Console.WriteLine("Actual: " + result.Value);
        ...
    }
    else
    {
        if (result.HasFaultException)
        {
            Console.WriteLine("Service operation threw a fault: " + result.Exception.ToString());
        }
        else if (result.HasException)
        {
            Console.WriteLine("An unexpected error occurred while calling the service operation: " + result.Exception.ToString());
        }
    }
}

Use Your Own Retry Routine Or Library

An example implementation of IRetryPolicyFunction is the RetryPolicyAdapter in the NuGet library ChannelAdam WCF Library Extensions for the Microsoft Practices TransientFaultHandling.Core Library.

If you want to use some other retry library, simply create your own class that implements IRetryPolicyFunction and set it into the ServiceConsumerFactory or a ServiceConsumer.

Exception Behaviour Strategy For Retry Attempts

The Exception Behaviour Strategy interface IServiceConsumerExceptionBehaviourStrategy includes a method named PerformRetryPolicyAttemptExceptionBehaviour. This method is executed when a retry policy is in use and an exception has occurred. When called, this method provides you with the count of the current retry attempt.



comments powered by Disqus