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:
- Setting the static property
DefaultRetryPolicy
on theServiceConsumerFactory
class - which will then apply to all new created instances ofServiceConsumer
; - Specifying the retry policy in one of the Create() method overloads on the
ServiceConsumerFactory
; or - By setting the
RetryPolicy
property directly on an instance ofServiceConsumer
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
1using ChannelAdam.ServiceModel;
2using Microsoft.Practices.TransientFaultHandling;
3
4...
5
6var microsoftRetryStrategy = new Incremental(5, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2));
7var microsoftRetryPolicy = new RetryPolicy<SoapFaultWebServiceTransientErrorDetectionStrategy>(microsoftRetryStrategy);
8
9ServiceConsumerFactory.DefaultRetryPolicy = microsoftRetryPolicy.ForServiceConsumer(); // extension method
10// or ServiceConsumerFactory.DefaultRetryPolicy = new ChannelAdam.TransientFaultHandling.RetryPolicyAdapter(microsoftRetryPolicy);
11// or ServiceConsumerFactory.DefaultRetryPolicy = (ChannelAdam.TransientFaultHandling.IRetryPolicyFunction)microsoftRetryPolicy;
12// Because DefaultRetryPolicy has been set, the retry policy will be applied when AddIntegers() is called.
13
14using (var service = ServiceConsumerFactory.Create<IFakeService>(() => new FakeServiceClient()))
15{
16 try
17 {
18 int actual = service.Operations.AddIntegers(1, 1);
19 }
20 catch (FaultException fe)
21 {
22 Console.WriteLine("Service operation threw a fault: " + fe.ToString());
23 }
24 catch (Exception ex)
25 {
26 Console.WriteLine("Technical error occurred while calling the service operation: " + ex.ToString());
27 }
28}
2. Sample Retry Code Using the Consume Method
The retry policy works exactly the same when the Consume() method is used.
1using ChannelAdam.ServiceModel;
2using Microsoft.Practices.TransientFaultHandling;
3
4...
5
6var microsoftRetryStrategy = new Incremental(5, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2));
7var microsoftRetryPolicy = new RetryPolicy<SoapFaultWebServiceTransientErrorDetectionStrategy>(microsoftRetryStrategy);
8
9ServiceConsumerFactory.DefaultRetryPolicy = microsoftRetryPolicy.ForServiceConsumer(); // extension method
10// or ServiceConsumerFactory.DefaultRetryPolicy = new ChannelAdam.TransientFaultHandling.RetryPolicyAdapter(microsoftRetryPolicy);
11// or ServiceConsumerFactory.DefaultRetryPolicy = (ChannelAdam.TransientFaultHandling.IRetryPolicyFunction)microsoftRetryPolicy;
12// Because DefaultRetryPolicy has been set, the retry policy will be applied when AddIntegers() is called.
13
14using (var service = ServiceConsumerFactory.Create<IFakeService>(() => new FakeServiceClient()))
15{
16 var result = service.Consume(operation => operation.AddIntegers(1, 1));
17
18 if (result.HasNoException)
19 {
20 Console.WriteLine("Actual: " + result.Value);
21 ...
22 }
23 else
24 {
25 if (result.HasFaultException)
26 {
27 Console.WriteLine("Service operation threw a fault: " + result.Exception.ToString());
28 }
29 else if (result.HasException)
30 {
31 Console.WriteLine("An unexpected error occurred while calling the service operation: " + result.Exception.ToString());
32 }
33 }
34}
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