Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / C#

Transient Fault Handling Application Block Simplified

Rate me:
Please Sign up or sign in to vote.
5.00/5 (11 votes)
6 May 2014CPOL3 min read 44.5K   361   11   9
How to use the Transient Fault Handling Application Block

Image 1

What is Transient Fault Handling Application Block

Microsoft documentation says it best:

The Microsoft Enterprise Library Transient Fault Handling Application Block lets developers make their applications more resilient by adding robust transient fault handling logic. Transient faults are errors that occur because of some temporary condition such as network connectivity issues or service unavailability. Typically, if you retry the operation that resulted in a transient error a short time later, you find that the error has disappeared.

Different services can have different transient faults, and different applications require different fault handling strategies. The Transient Fault Handling Application Block encapsulates information about the transient faults that can occur when you use the following Windows Azure services in your application:

  • SQL Azure
  • Windows Azure Service Bus
  • Windows Azure Storage
  • Windows Azure Caching Service

The Transient Fault Handling Application Block enables the developer to select from the following retry strategies:

  • Incremental
  • Fixed interval
  • Exponential back-off

The Enterprise Library Transient Fault Handling Application Block includes the following features:

  • You can select from an extensible collection of error detection strategies for cloud-based services, and an extensible collection of retry strategies.
  • You can use the graphical Enterprise Library configuration tool to manage configuration settings.
  • You can extend the block by adding error detection strategies for other services or by adding custom retry strategies.

Background

When I read the above, I thought to myself: "OK, Azure is all well and good but what about my non-cloud console application that was written 5 years ago and does experience transient errors from time to time. Can I use the Transient Fault Handling Application Block in my old application and how much change will it require?"

It turned out that it's not a big deal.

Add a reference to the Transient Fault Handling Application Block assemblies

Right-click your project in Solution Explorer and select Manage NuGet Packages. Type Transient Fault Handling in the Search box and click Search. Select Enterprise Library - Transient Fault Handling Application Block package. After you click Install, a set of necessary assemblies and references that support the Transient Fault Handling Application Block will be added to the project.

Add the code. There are 3 steps to write code that uses the Transient Fault Handling Application Block.

  1. Define the retry strategy with a specified number of retry attempts and an incremental time interval between retries.
  2. Define the retry policy - mechanism for unreliable actions and transient conditions.
  3. Wrap any functions that may experience transient faults with the ExecuteAction delegate to repeatedly execute the specified action while it satisfies the current retry policy.
C#
static void Main(string[] args)
{
    try
    {
        // Step 1
        var retryStrategy = new Incremental(RETRY_COUNT, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2));

        // Step 2 
        var retryPolicy = new RetryPolicy<CustomTransientErrorDetectionStrategy>(retryStrategy);

        // Step 3
        retryPolicy.ExecuteAction(NavigateTo);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
}

// This is the code that may experience transient errors
static private void NavigateTo()
{
    Console.WriteLine(DateTime.Now);

    WebClient wc = new WebClient();
    wc.DownloadString("c:\\temp.txt");
}

Now what is a template parameter to the RetryPolicy class - CustomTransientErrorDetectionStrategy?

ITransientErrorDetectionStrategy is an interface which must be implemented by custom components responsible for detecting specific transient conditions and CustomTransientErrorDetectionStrategy class implements this interface.

ITransientErrorDetectionStrategy has only one method - bool IsTransient(Exception ex) and its job is to determine if the specified exception represents a transient failure that should be retried. It should return true if the exception is transient and the code should be repeated and false otherwise.

The implementation could be as simple as this:

C#
internal class CustomTransientErrorDetectionStrategy : ITransientErrorDetectionStrategy
{
    public bool IsTransient(Exception ex)
    {
        if (ex is WebException)
            return true;
        return false;
    }
}

Conclusion

As you can see, handling the transient errors with the help of the Transient Fault Handling Application Block can be achieved very easily by following just a few steps explained above.

Demo Application

The demo project was developed with Visual Studio 2013 and .NET framework 4.5 and it has the code described above.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNice, I actually do something like this myself using Rx (codebase uses lots of Rx) Pin
Sacha Barber9-Oct-14 0:27
Sacha Barber9-Oct-14 0:27 
AnswerRe: Nice, I actually do something like this myself using Rx (codebase uses lots of Rx) Pin
Igor Vigdorchik9-Oct-14 6:15
Igor Vigdorchik9-Oct-14 6:15 
GeneralRe: Nice, I actually do something like this myself using Rx (codebase uses lots of Rx) Pin
Sacha Barber17-Oct-14 3:23
Sacha Barber17-Oct-14 3:23 
GeneralGood Article to Demonstrate on Transient Fault Handling Pin
Sreenivas.pagadala1-Sep-14 15:04
Sreenivas.pagadala1-Sep-14 15:04 
QuestionDTC Pin
lusgon27-May-14 11:51
lusgon27-May-14 11:51 
AnswerRe: DTC Pin
Igor Vigdorchik27-May-14 12:33
Igor Vigdorchik27-May-14 12:33 
QuestionNice Igor! Pin
Volynsky Alex6-May-14 22:25
professionalVolynsky Alex6-May-14 22:25 
AnswerRe: Nice Igor! Pin
Igor Vigdorchik7-May-14 3:40
Igor Vigdorchik7-May-14 3:40 
GeneralRe: Nice Igor! Pin
Volynsky Alex7-May-14 10:02
professionalVolynsky Alex7-May-14 10:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.