Azure Storage Queue example in C#

In this post, we learn how to work with azure storage Queues, and Azure ServiceBus. let’s add following namespace in reference in your project, you may need to install few assembly from Nuget package manager.

using ServiceBus=Microsoft.Azure.ServiceBus;
using Azure.Storage.Queues;
using Microsoft.WindowsAzure.Storage.Queue;
using System.Configuration; // Namespace for ConfigurationManager
using Azure.Storage.Queues.Models; // Namespace for PeekedMessage

If you ever come across situation when you need to develop an application that will work with azure storage queue and service bus queue, then you may find two Queue class under two different namespace, and we may wonder why!

Use of Storage Queue and ServiceBus Queue

Two queue class are made for different purpose, here are few points can be taken into consideration!

  • Storage Queue: This Queue class used for storing large number of message, which does not need any automatic processing.
    1. We can store large number of queues in azure storage (over 80 gigabytes)
    2. Each message can contain message size up to 64 kb
    3. when we need to store any type of application log message in this queue storage
  • ServiceBus Queue: Service queue is used for transactional use, think of developing microservice scenario.
    1. when storage will not be more than 80 GB.
    2. when in application we need automatic duplicate detection.
    3. when we need FIFO (first in first out) in our queues.
    4. when application need feature like having message without making a pull call to queue.

How to work with Queues in Azure Storage

Assuming, you are already familiar with working with azure storage like blob, file share, cloud table etc, where we create storage account instance based on storage connection string.

In following method, we create a queue using QueueClient class object and with “SendMessage” send message to queue client.

async Task<string> CreateMyQueue(string queueName)
{
	string _result = $"Create Queue '{queueName}' request received";
   
// Get the connection string from app settings
string _connectionString = _configuration.GetSection("AzureStorage")["ConnectionString"];

// Instantiate a QueueClient class object
QueueClient queueClient = new QueueClient(_connectionString, queueName);

// Create the queue
await queueClient.CreateIfNotExistsAsync();
_result = $"Queue '{queueName}' created";

queueClient.SendMessage($"test message of '{queueClient.Name}' QueueClient object at '{DateTime.Now.Millisecond}' ");
queueClient.SendMessage($"test message of '{queueClient.Name}' QueueClient object at '{DateTime.Now.Millisecond}' ");

_result = $"Queue '{queueName}' SendMessage executed";
	 
	return _result;
}

In above example i have added some messages at the time of queue creation, We can store any number of messages in each queue; each message size can be up to 64 KB.

We can store messages in azure queue and access from different applications with appropriate credentials, think of situation like any enterprise business, where you have different type of applications and storing all log messages in queue storage, then based on requirement you are picking any queue from any application and reading or clearing those messages.

Here is how you can retrieve all queues from your storage!

async Task<List<CloudQueue>> GetQueueList()
{
	IEnumerable<CloudQueue> qs = new List<CloudQueue>();
	string _connectionString = _configuration.GetSection("AzureStorage")["ConnectionString"];

	string _storageQUri = "https://yourStorageName.queue.core.windows.net/";
	CloudStorageAccount _storageAccount = CloudStorageAccount.Parse(_connectionString);

	Uri _uri = new Uri(_storageQUri);
	StorageUri _storageUri = new StorageUri(_uri);

	CloudQueueClient _cQClient = new CloudQueueClient(_storageUri, _storageAccount.Credentials);

	QueueContinuationToken token = new QueueContinuationToken();
	var result= await _cQClient.ListQueuesSegmentedAsync(token);

	if(result!=null)
		qs = result.Results;

	return  qs.ToList();
}

We can loop through the above queue list and retrieve each queue to work with! we can get the message as byte array or as string.

foreach (CloudQueue cq in Model.MyQList)
{
	@cq.Name;
	
	IEnumerable<CloudQueueMessage> _msgs = cq.PeekMessagesAsync(10).Result;
	if (_msgs != null)
	{
		foreach (CloudQueueMessage cqm in _msgs)
		{
			@cqm.Id
		}
	}
}    

Similarly using QueueClient object we can pick messages from Queue and read them. note, while looping through message in abve code, all message property is working fine, except @cqm.AsString property, which has the message content.

Peeked Messages from any Queue

There is another way we can read message from queue, is with QueueClient object.

We can perfrom all operations like creating queue, inserting a message in queue. update message in queue, deleting message and deleting queue.

To create a new instance of queue client we need the storage connection string and a queue name.

QueueClient queueClient = new QueueClient(_connectionString, queueName);

You can learn more about azure storage queue concept.

One queue can have any number of messages, here is an example of how to retrieve message from one queue.

public PeekedMessage[] PickMyMessage(string queueName)
{
    PeekedMessage[] resultMessageArray=null;
             
    string _connectionString = _configuration.GetSection("AzureStorage")["ConnectionString"];
           
    QueueClient queueClient = new QueueClient(_connectionString, queueName);
             
    if (queueClient.Exists())
    {
        // Peek at the next message
        resultMessageArray = queueClient.PeekMessages();               
    }
    return resultMessageArray;
}

We can retrieve and any particular message with new content.

    
QueueClient queueClient = new QueueClient(cString, queueName);

if (queueClient.Exists())
{
QueueMessage[] message = queueClient.ReceiveMessages();
                                
queueClient.UpdateMessage(message[0].MessageId,
        message[0].PopReceipt,
        "Update new contents here",
        TimeSpan.FromSeconds(60.0) 
    );

}
Dequeue Message from storage queue

Finally, we learn how to delete message from queue, we can delete one particular message or can delete the queue, so all the content inside it will be deleted.

QueueClient queueClient = new QueueClient(_connectionString, queueName);
if (queueClient.Exists())
{    QueueMessage[] message = queueClient.ReceiveMessages();
                                
    queueClient.DeleteMessage(message[0].MessageId,message[0].PopReceipt);
}


// we also can delet the queue client completely 
// all messages in it will be deleted 
queueClient.Delete();

With just QueueClient object we can do all queue realted operations! apat from fetching all queues from storage account, which i have explained GetQueueList method in this post.

 
Azure Cloud Tutorial
learn azure cloud development

Let's learn how to use Azure Cloud Storage system from Ap.net core c# application.

Data Analysis
learn Artificial Intelligence
Add Data to Azure Cloud Table
Learn Azure Cloud Development