Azure Messaging Solutions using Storage Queues

Azure Messaging Solutions using Storage Queues

In my previous article we have seen what is messaging and how a message differs conceptually from the event. If you want to read that article, below is the link.

In this article, we will see how Azure storage account should be created and how to manage queue in .Net code.

For following the steps mentioned in this article, you will need access to Azure Portal. If you do not have access, then you can create free account on Azure.

What are Storage Queues ?

Queue is First In First Out data structure.

Azure queue storage is a service for storing large number of messages. These messages can be accessed anywhere over HTTP or HTTPS via authenticated calls.

Below are few terms before you start on Storage Queues:

  • Storage Account: Storage queues are created under a storage account. All access for queues is controlled through storage account. You need storage account to create storage queue e.g. you need connection string of storage account to get queue reference OR you need storage account to create a new queue.
  • Queue: First in first out structure, which contains set of messages. Single message can be up to 64 KB. This queue can contain millions of messages depending on storage account size limit. The queue can be identified using queue name. Queue name contains all lower case characters. Queue name should be unique within a storage account.
  • Message: A message, in any format, of up to 64 KB. The maximum time that a message can remain in the queue is seven days.
  • URL Format: Queues are addressable using the following URL format:
    https://<storage account><queue-name>

Create Storage Account

On Azure portal, you can create on “+ Create a resource” button, and then click on storage category and select Storage account.

Then storage account create panel will open. Provide resource group and storage account name. Then click on “Review and Create” button.

Azure PowerShell

Alternatively, you can use below PowerShell command to create Azure Storage Account

[code language=’powershell’] New-AzStorageAccount -ResourceGroupName $resourceGroup ` -Name “storagequickstart” ` -Location $location ` -SkuName Standard_LRS ` -Kind StorageV2 [/code]

Azure CLI

One more alternative, is to use Azure CLI command to create storage account. Below is the sample command:

[code language=’bash’] az storage account create \ –name storagequickstart \ –resource-group storage-quickstart-resource-group \ –location westus \ –sku Standard_LRS \ –kind StorageV2 [/code]

Storage Account Connection String

Multiple type of resources can be created in storage account. You can create Table, Files, Blob or Queue. These resources can be connected by using connection string and the URL.

As we already stated, the URL for queue is as follows:

https://<storage account><queue-name>

The connection string can be taken from Azure portal

Navigate to the Azure portal. Locate your storage account. In the Settings section of the storage account overview, select Access keys.

Under AccessKeys you will be able to find 2 keys and 2 connection strings. You can use any of the connection string to connect.

The keys and connection strings are hidden in below snapshot. The connection string would be in the below format:

[code language=”text”] DefaultEndpointsProtocol=https;AccountName=demoaccountstorage;AccountKey=; [/code]

.Net Code

Below is .Net code which explains all type of interactions you may require for messaging solutions. Below is the sample code and list of classes to be used.

  • CloudStorageAccount – for getting queue reference
  • CloudQueueClient – for getting queue reference
  • CloudQueue – for operations on queue
  • CloudQueueMessage – for operations on messages
[code language=’csharp’] // Get connection string from configuration string storageConnectionString = ConfigurationManager.AppSettings[“StorageAccountConnString”]; // Check whether the connection string can be parsed. CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString); // Create a queue client for interacting with the queue service CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient(); // Create queue with given queue name CloudQueue queue = queueClient.GetQueueReference(queueName); // This will create queue if it does not exist yet. await queue.CreateIfNotExistsAsync(); // Adding message to queue and Expiration time to 14 days. CloudQueueMessage message = new CloudQueueMessage(“Hello, World”); await queue.AddMessageAsync(message, new TimeSpan(14,0,0,0), null, null, null); // Peek at message at front. CloudQueueMessage peekedMessage = await queue.PeekMessageAsync(); // Retrieve the message at the front of the queue. CloudQueueMessage retrievedMessage = await queue.GetMessageAsync(); // Delete the message within the period of invisibility. await queue.DeleteMessageAsync(retrievedMessage); // Delete queue if already exists await queue.DeleteIfExistsAsync(); [/code]

Azure Storage Explorer

Azure storage explorer is a desktop client which you can use to check contents of storage account. It is available as free download. In order to check those contents, you have to login to your Azure subscription after opening this tool and it will show all your storage accounts.

In this article, we have seen how to use storage queues and how easy to use it. Please do comment and let me know your thoughts.

Leave a Reply