Export (0) Print
Expand All

Specifying Retry Strategies in the Configuration

On this page:
Usage Notes

If your solution makes a large number of calls to methods that require retry logic, you can define the retry strategies in the application configuration. This helps to ensure that you use consistent retry policies, and makes it easier to modify retry settings without having to recompile your code.

The following snippet from the application configuration file shows some example retry strategies.

<RetryPolicyConfiguration defaultRetryStrategy="Fixed Interval Retry Strategy"
    defaultSqlConnectionRetryStrategy="Backoff Retry Strategy"
    defaultSqlCommandRetryStrategy="Incremental Retry Strategy"
    defaultAzureStorageRetryStrategy="Fixed Interval Retry Strategy"
    defaultAzureServiceBusRetryStrategy="Fixed Interval Retry Strategy">
    <incremental name="Incremental Retry Strategy" retryIncrement="00:00:01"
        retryInterval="00:00:01" maxRetryCount="10" />
    <fixedInterval name="Fixed Interval Retry Strategy" retryInterval="00:00:01"
        maxRetryCount="10" />
    <exponentialBackoff name="Backoff Retry Strategy" minBackoff="00:00:01"
        maxBackoff="00:00:30" deltaBackoff="00:00:10" maxRetryCount="10" 
        fastFirstRetry="false"/>
</RetryPolicyConfiguration>
Hh680900.note(en-us,PandP.50).gifNote:
You can define retry strategies using the Enterprise Library configuration tool. For more information, see the topic "Entering Configuration Information."

The following code sample shows how you can select the retry strategy named "Incremental Retry Strategy" from the configuration settings and use it when you invoke a method that requires a retry policy.

using Microsoft.Practices.TransientFaultHandling;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling;

...

// Get an instance of the RetryManager class.
var retryManager = EnterpriseLibraryContainer.Current.GetInstance<RetryManager>();

// Create a retry policy that uses a retry strategy from the configuration.
var retryPolicy = retryManager.GetRetryPolicy
  <StorageTransientErrorDetectionStrategy>("Incremental Retry Strategy");
            
// Receive notifications about retries.
retryPolicy.Retrying += (sender, args) =>
    {
        // Log details of the retry.
        var msg = String.Format("Retry - Count:{0}, Delay:{1}, Exception:{2}",
            args.CurrentRetryCount, args.Delay, args.LastException);
        Trace.WriteLine(msg, "Information");
    };

try
{
  // Do some work that may result in a transient fault.
  var blobs = retryPolicy.ExecuteAction(
    () =>
    {
        // Call a method that uses Windows Azure storage and which may
        // throw a transient exception.
        this.container.ListBlobs();
    });
}
catch (Exception)
{
  // All the retries failed.
}

The client app can also obtain a reference to a RetryPolicy instance by using the RetryPolicyFactory class. This approach is provided for backwards compatibility with the Transient Fault Handling Application Framework.

Usage Notes

  • To use the EnterpriseLibraryContainer class, your project should include references to the Microsoft.Practices.EnterpriseLibrary.Common and Microsoft.Practices.ServiceLocation assemblies. For more information about creating and referencing Enterprise Library objects, see the topic "Creating and Referencing Enterprise Library Objects" on MSDN.
  • The GetRetryPolicy method takes a parameter that identifies the retry strategy to load from the configuration. If you do not supply a parameter, the method will load the global default retry strategy.
  • If you are using SQL Azure, you can use either the GetDefaultSqlConnectionRetryPolicy or the GetDefaultSqlCommandRetryPolicy method to load one of these default policies from the configuration.
  • In the RetryManager class, the GetDefaultAzureCachingRetryPolicy method returns the default retry policy for the Windows Azure cache from the configuration.
  • In the RetryManager class, the GetDefaultAzureServiceBusRetryPolicy method returns the default retry policy for the Windows Azure Service Bus from the configuration.
  • In the RetryManager class, the GetDefaultAzureStorageRetryPolicy method returns the default retry policy for the Windows Azure storage from the configuration.
  • This example also illustrates the use of an overloaded version of the ExecuteAction method that returns a result.
  • You should be wary of trying to load retry strategies using the RetryPolicyFactory or RetryManager classes in the web role OnStart event because the process that runs the web role does not read the web.config file. There are a number of approaches that you can use to work around this issue:
    • Package and deploy the web.config file as a part of the web role deployment. You can copy it manually or set the project to copy it automatically through the "Copy if newer" setting (just like any other file). Then use the following code in the web role to initialize the Enterprise Library container.
      EnterpriseLibraryContainer.Current = EntepriseLibraryContainer.ConfigureDefaultContainer(new FileConfigurationSource("web.config", false)); 
      
    • Keep the retry strategy configuration in a separate app.config file and reference it using a FileConfigurationSource section in the web.config file. Note that connection strings are always retrieved from the main configuration file, so they would need to be duplicated. For more information, see the "Configuration Sources Hands-On Lab for Enterprise Library" in the "Hands-on Labs for Microsoft Enterprise Library 5.0."
    • Keep each shared section in a separate config file and use the configSource attribute to point to them.

Last built: June 7, 2012

Show:
© 2014 Microsoft