Delen via


Using Asynchronous Methods with Retries

Retired Content

This content and the technology described is outdated and is no longer being maintained. For more information, see Transient Fault Handling.

patterns & practices Developer Center

On this page:
Usage Notes

You can use the Transient Fault Handling Application Block to invoke asynchronous methods that require a retry policy.

The following code example shows how to invoke the pair of asynchronous methods BeginDeleteIfExists and EndDeleteIfExists. The EndDeleteIfExists method returns a Boolean value to indicate whether the blob was deleted.

// Define your retry strategy: retry 5 times, starting 1 second apart
// and adding 2 seconds to the interval each retry.
var retryStrategy = new Incremental(5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));

// Define your retry policy using the retry strategy and the Azure storage
// transient fault detection strategy.
var retryPolicy = new RetryPolicy<StorageTransientErrorDetectionStrategy>(retryStrategy);

// 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");
    };

// Do some work that may result in a transient fault.
retryPolicy.ExecuteAction(
    ac =>
    {
        // Invoke the begin method of the asynchronous call.
        this.DataBlob.BeginDeleteIfExists(ac, null);
    },
    ar =>
    {
        // Invoke the end method of the asynchronous call.
        return this.DataBlob.EndDeleteIfExists(ar);
    },
    v =>
    {
        // Action to perform if the asynchronous operation
        // succeeded.
        if (v)
            {
                Trace.WriteLine("Blob deleted successfully", "Information");
            }
            else
            {
                Trace.WriteLine("The Blob was not deleted", "Information");
            }
    },
    e =>
    {
        // Action to perform if the asynchronous operation
        // failed after all the retries.
        var msg = String.Format("Delete blob failed: {0}", e.ToString());
        Trace.WriteLine(msg, "Error");
    });

Usage Notes

  • There are two overloaded versions of the ExecuteAction method that you can use to invoke asynchronous methods. One invokes methods that do not have a return value and one invokes methods that do have a return value. If the asynchronous method returns a value, it is passed to the success handler.
  • You could use the value that is passed to the success handler to begin another asynchronous operation. For example, if deleting the blob succeeded, begin a copy from blob operation.
  • The success handler is called when the asynchronous operation succeeds. The failure handler is called if the asynchronous operation cannot be completed despite the retry attempts.

Next Topic | Previous Topic | Home

Last built: June 7, 2012