Windows Azure Queue Storage Service Polling Task

February 19, 2013 — 4 Comments


The screenshot above is of my test console, showing that the PollingTask is effectively applying the back off strategy. If the QueueWorker finds new messages to process the back off delay will be reset to zero.

The code from this Post is part of the Brisebois.WindowsAzure NuGet Package

To install Brisebois.WindowsAzure, run the following command in the Package Manager Console

PM> Install-Package Brisebois.WindowsAzure

Get more details about the Nuget Package.

The code below uses a QueueWorker which implements PollingTask<TWorkItem> to dequeue 32 messages from a test queue, it then processes the messages in parallel. If the action is successful, the message is deleted from the test queue. When a message is read from the queue, it will not be visible for the next minute. If for some reason an exception is thrown during the action, the message will become visible and will be dequeued a second time. Once dequeued for a second time, the message dequeue count will be check. If the dequeue count is greater or equal to 1, the message will be removed from the test queue and be placed on the poison-test queue.

If it does not find messages in the queue it uses an exponential back off strategy to reduce the number of transactions made on Windows Azure Queue Storage Service. Without the back off strategy, the service would continuously make requests to check if new messages needed to be processed.

Consequences of making too many requests to Queue Storage Service Include

  • The service might throttle your requests
  • The cost of operation would be significantly higher than it should be


class Program
    static void Main(string[] args)
        var w = new MessageQueueWorker("StorageConnectionString",

Implementing the MessageQueueWorker mostly consists of implementing a Report method. This method is used to trace your code through diagnostics or logging frameworks. The OnExecution method is where the real magic happens. This is where you can implement message specific logic. The QueueWorker handles all the boiler plate interactions with the source and poison queues.

public class MessageQueueWorker: QueueWorker
    public MessageQueueWorker(string accountConnectionStringName, 
                              string queueName, 
                              string poisonQueueName, 
                              int maxAttempts = 10,
                              int visibilityTimeOutInMinutes = 10)
        : base(accountConnectionStringName, 

    protected override void Report(string message)
    protected override void OnExecuting(CloudQueueMessage workItem)
        //Do some work 
        var message = workItem.AsString;
        //Used for testing the poison queue
            throw new Exception(message);


The QueueWorker implements the PollingTask in order to read messages from a Queue Storage Service. Working with Queues in Windows Azure is quite common. In many circumstances I try to make specialized Queues. This means that I store messages that can be acted upon by a single consumer. Doing so requires me to create multiple QueueWorker instances in the same Worker Role. Having many specialized QueueWorker implementations running side by side also has it benefits. Each QueueWorker can be tuned differently. Some can process 32 messages in parallel while others can process 4, this gives you a lot of free way when it comes to consuming a maximum amount of the available resources. Furthermore, if the demand for your services grow, you can easily break apart the collection of QueueWorker instances and redistribute and reorganize them over more Worker Roles.

The QueueWorker handles all the queue specific work. This is where messages are dequeued, deleted and placed in the poison queue. Messages are dequeued with a visibility timeout of 10 minutes by default and messages are considered as poison after 10 dequeue attemps. Both of these parameters can be altered through the constructor. 

An Exponential RetryPolicy is used to make the QueueWorker tolerant transient faults.

public abstract class QueueWorker : PollingTask<CloudQueueMessage>
    private readonly CloudQueueClient client;
    private readonly CloudQueue poisonQueue;
    private readonly CloudQueue queue;
    private readonly TimeSpan visibilityTimeout;
    private readonly int maxAttempts;

    protected QueueWorker(string connectionString, 
                        string queueName, 
                        string poisonQueueName,
                        int maxAttempts = 10,
                        int visibilityTimeoutInMinutes = 10)
        this.maxAttempts = maxAttempts;

        var cs = CloudConfigurationManager.GetSetting(connectionString);
        var account = CloudStorageAccount.Parse(cs);

        client = account.CreateCloudQueueClient();

        client.RetryPolicy = new ExponentialRetry(new TimeSpan(0, 0, 0, 2), 10);
        queue = client.GetQueueReference(queueName);

        poisonQueue = client.GetQueueReference(poisonQueueName);

        visibilityTimeout = TimeSpan.FromMinutes(visibilityTimeoutInMinutes);
    protected override void Execute(CloudQueueMessage workItem)
        if (workItem.DequeueCount > maxAttempts)


    protected abstract void OnExecuting(CloudQueueMessage workItem);

    private void PlaceMessageOnPoisonQueue(CloudQueueMessage workItem)
        var message = new CloudQueueMessage(workItem.AsString);

    protected override void Completed(CloudQueueMessage workItem)
        catch (Exception ex)

    protected override ICollection<CloudQueueMessage> GetWork()
        return queue.GetMessages(32, visibilityTimeout)

Before executing any of this code be sure to include the PollingTask<TWorkItem> class in your project. Further more be sure to configure the storage connection string in your App.Config or Cloud Configurations.

    <add key="StorageConnectionString" value="..." />

4 responses to Windows Azure Queue Storage Service Polling Task



    I installed the nuget-package. It adds a lot of dependencies to my project, yet no code or libraries of your own. How can I find the code? Is it installed on disk somewhere?



    It’s a very good idea, but Thread.Sleep() will block an entire thread, they say.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s