fbpx

Azure Storage Queues provide an easy to implement messaging communication mechanism for reliably passing messages to/from different components of a single application or multiple applications. Storage Queues have an HTTP/HTTPS API to allow for support of any development platform; however, Microsoft does provide a .NET SDK to make it even easier to implement from C# or other .NET languages.


Azure Storage Nuget Package

The API’s for working with the Microsoft Azure Storage Queue are contained within the “WindowsAzure.Storage” Nuget package. Adding a reference to the Nuget package will easily pull in all necessary dependencies that will enable the application to communicate, manage and work with the Microsoft Azure Storage Queue.

Windows Azure Storage Nuget Package
Install-Package WindowsAzure.Storage Nuget Package

Once a reference to the Microsoft Azure Storage Nuget Package is added tot he Visual Studio project, then everything will be ready to start coding against the Azure Storage Queues.

API Namespaces

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Queue;

Storage Queue Connection String

The Microsoft Azure Storage Queue connection string can be stored in the Azure service configuration or in the application config file (app.config or web.config).

Here’s an example of retrieving the connection string from the Azure service configuration:

var storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("MyStorageConnString")
);

Here’s an example of retrieving the connection string from the web.config or app.config file with it being stored within the Connection Strings section of the configuration file:

using System.Configuration;

var connString = ConfigurationManager
.ConnectionStrings("MyStorageConnString")
.ConnectionString;
var storageAccount = CloudStorageAccount.Parse(connString);

The “storageAccount” variable instantiated above will be used / reference in the rest of the code examples within this article to keep things brief and not duplicate the above code unnecessarily.

Create Storage Queue from .NET

Using the CloudStorageAccount and CloudQueueClient objects, it only take a couple lines of code to both get a reference to an Azure Storage Queue, and create the Queue if it does not exist yet.

Here’s an example of creating an Azure Storage Queue from .NET, that also creates the Queue if it does not exist yet:

// create Queue Client
var client = storageAccount.CreateCloudQueueClient();

// get Queue reference
var queue = client.GetQueueReference("myqueue");

// create Queue if it doesn't yet exist
queue.CreateIfNotExists();

Send Messages to the Queue

The CloudQueueMessage class is used as a container for placing the contents of a message that will be sent to the Azure Storage Queue. The message contents can contain either a UTF-8 encoded string or a byte array.

// create Queue client
var storageAccount.CreateCloudQueueClient();

// get Queue reference
var queue = client.GetQueueReference("myqueue");

// create Queue if it doesn't yet exist
queue.CreateIfNotExists();

// Create new Message
var message = new CloudQueueMessage("Hello Azure!");

// Add / Send Message to the Queue
queue.AddMessage(message);

Receive Single Message from Queue

The simplest method of retrieving messages from the Queue is to use the “PeekMessage” method on the CloudQueue object instance. This will pull the next meesage in the Queue for your code to process / handle.

Here’s an example of fetching a single message from the Azure Storage Queue:

// create Queue client
var storageAccount.CreateCloudQueueClient();

// get Queue reference
var queue = client.GetQueueReference("myqueue");

// create Queue if it doesn't yet exist
queue.CreateIfNotExists();

// retrieve next Message from Queue
var message = queue.PeekMessage();

// get message contents
var contents = message.AsString;

Receive Message Batch from Queue

The CloudQueue class also exposes functionality offering the ability to fetch a batch / multiple message from the Azure Storage Queue using the “GetMessages” method.

Here’s a simple example of fetching a batch of messages:

// create Queue client
var storageAccount.CreateCloudQueueClient();

// get Queue reference
var queue = client.GetQueueReference("myqueue");

// create Queue if it doesn't yet exist
queue.CreateIfNotExists();

// Fetch a batch of 10 messages
var batch = queue.GetMessages(10, TimeSpan.FromMinutes(2));

foreach(CloudQueueMessage msg in batch)
{
// process message

// Delete message from Queue after processing
queue.DeleteMessage(msg);
}

Delete a Message Queue

The CloudQueue class also has the “Delete” method that allows for an Azure Storage Queue to be programmatically deleted from .NET code.

Here’s a simple example of deleting an Azure Storage Queue from .NET:

// create Queue client
var storageAccount.CreateCloudQueueClient();

// get Queue reference
var queue = client.GetQueueReference("myqueue");

// delete Queue
queue.Delete();

Microsoft MVP

Chris Pietschmann is a Microsoft MVP, HashiCorp Ambassador, and Microsoft Certified Trainer (MCT) with 20+ years of experience designing and building Cloud & Enterprise systems. He has worked with companies of all sizes from startups to large enterprises. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.
HashiCorp Ambassador Microsoft Certified Trainer (MCT) Microsoft Certified: Azure Solutions Architect