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.
- Define the retry strategy with a specified number of retry attempts and an incremental time interval between retries.
- Define the retry policy - mechanism for unreliable actions and transient conditions.
- 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.
static void Main(string[] args)
{
try
{
var retryStrategy = new Incremental(RETRY_COUNT, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2));
var retryPolicy = new RetryPolicy<CustomTransientErrorDetectionStrategy>(retryStrategy);
retryPolicy.ExecuteAction(NavigateTo);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
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:
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.