Thursday, January 8, 2015

Working with Storage Queue using C# Code in Azure

Queues, table storage and blob storage are unified under a Storage Account and you probably have to create one now. From the Azure Management portal click the New button in the lower left hand corner and select Data Services, Storage, Quick Create. Just specify a name, the location of your storage and your preferred replication strategy.
2215-Queues-2-Fig-01-storageaccount-620.
Figure 1: Creating a storage account

Set up your project

For the first API tests I’ll create a simple Console application to demo a few things. To access your queues from .NET you will need the Storage API which can be referenced easily as a NuGet package named Windows Azure Storage (the sample code uses Storage API 4.3.0).
2215-Queues-2-Fig-02-nuget-620.png
Figure 2: Adding storage reference via Nuget

Create a queue

Let’s create a queue. Of course you can do that with your favorite tool too (e.g. Visual Studio or Azure Management Studio), but as you’ve seen already it’s also possible through the API. The main class for your communication is called CloudQueueClient and an instance is provided through a factory method from the CloudStorageAccount class.
To access your storage account you will need StorageCredentials: take the previously specified name of your storage account and one of the access keys that you can find for your storage account in the Azure portal (How to: View, copy, and regenerate storage access keys). The second parameter of the CloudStorageAccount constructor states if you want to use HTTPS for the communication.
With the CloudQueueClient.GetQueueReference method you can get a reference to your queue. Before working with the queue, you have to create it by using CreateAsync or Think carefully if you want to spend this additional call every time you’re accessing your queue, usually you can setup your queue once and there’s no need to ensure the existence again and again.
Creating a queue with .NET SDK

Inserting messages

Next we want to insert a TaaS ticket request in our newly created queue. Queue messages can be stored as string or as byte array but be aware of the size limits and the base64 encoding of the .NET Storage API (see my previous article, “Adding a message: Don’t be chatty”).
I use Newtonsoft.Json to serialize my simple TicketRequest object and pass that JSON formatted string to the constructor of theCloudQueueMessage class. Just call AddMessageAsync on your queue instance and you’re done!
Inserting a message into the queue
There are some overloads for the AddMessageAsync method that enable you to specify additional options for the message, e.g. the maximum time to allow the message to be in the queue (time-to-live) or the initial invisibility delay. See MSDN for further information.

Processing and deleting messages

Worker roles are probably the best place to process queue messages. Worker roles can be run in parallel, you have auto-scaling features that can depend on queue sizes and they are quite easy to implement. I’ll show a realistic architecture in my last article about queues, but for now you can put the code in your Console application as well.