Software Design Blog

Journey of Rinat Abdullin

Exception Handling in Windows Azure

Did you know that exception handling action policies from the Lokad Shared Libraries are compatible with the retry policies of the StorageClient project in Windows Azure? That’s how simple it is to use them together:

var policy = ActionPolicy.Handle<StorageServerException>().Retry(5);
queueService.RetryPolicy = policy.Do;

Obviously, you can provide more complex handling and retrying logic here, than a simple “Retry 5 times on a StorageServerException”.

Here’s a larger piece of code that registers QueueStorage in the Autofac IoC container, optionally supplying it with our global action policy:

var queueUri = new Uri(QueueEndpoint);
var accountInfo = new StorageAccountInfo(queueUri, null, 
  AccountName, AccountKey);

builder.Register(c =>
{
  var queueService = QueueStorage.Create(accountInfo);
  // if we have an action policy around, apply it
  ActionPolicy policy;
  if (c.TryResolve(out policy))
  {
    queueService.RetryPolicy = policy.Do;
  }                 
  return queueService;
});

It works in a similar way for the other storage classes - blob containers and tables.

Advantages of this approach are:

  • you just have to define a single module that registers storage classes, based on the configuration, and then it could be reused in multiple applications (worker roles, web roles, external applications that integrate with Azure etc);
  • in all scenarios, any given application policy has to be defined just once - in the wire-up code for a given application. Then it will be applied to all related reliability layers automatically.

More information on exception handling action policies is available in a separate article.