Take advantage of Azure Storage queues to store large numbers of messages for asynchronous or even distributed processing. Credit: Gonin / Getty Images A queue is a data structure that works on a FIFO (first in first out) basis. Items are inserted at the rear of the queue and removed from the front. The term “Enqueue” denotes the operation that inserts data in the queue, while the term “Dequeue” denotes the removal of data from the queue. Azure supports two types of queues: the Azure Storage queues and Azure Service Bus queues. This article discusses Azure Storage queues, how they differ from the Azure Service Bus queues, and how to work with Azure Storage queues programmatically. To work with the code examples provided in this article, you should have Visual Studio 2019 installed in your system. If you don’t already have a copy, you can download Visual Studio 2019 here. Create a console application project in Visual Studio First off, let’s create a .NET Core console application project in Visual Studio. Assuming Visual Studio 2019 is installed in your system, follow the steps outlined below to create a new .NET Core console application project in Visual Studio. Launch the Visual Studio IDE. Click on “Create new project.” In the “Create new project” window, select “Console App (.NET Core)” from the list of templates displayed. Click Next. In the “Configure your new project” window shown next, specify the name and location for the new project. Click Create. This will create a new .NET Core console application project in Visual Studio 2019. We’ll use this project to work with Azure Storage queues in the subsequent sections of this article. What is Azure Queue Storage? Azure Queue Storage is a Microsoft Azure cloud service that enables you to store vast numbers of messages for processing. You can take advantage of authenticated HTTP or HTTPS calls to access messages from anywhere in the globe in a secure manner. The maximum size of a queue message is 64 KB. A queue in Azure Queue Storage can store huge numbers of messages (even millions), up to a maximum storage capacity of 200 TB. Like your other Azure Storage data objects, including blobs, files, and tables, your queues are stored in your Azure Storage account. The Azure Storage account gives you a distinct namespace for your Azure Storage data, which you can access over HTTP or HTTPS from anywhere on the world. Azure Storage queues vs. Azure Service Bus queues Azure Service Bus is a scalable message fabric built on Azure Storage that provides reliable messaging as an Azure cloud service. You can use it for three different types of messaging: service relays between on-prem and cloud environments; topics for one-to-many publish/subscribe communications; and queues. In this section, we’ll example the differences between Azure Storage queues and Azure Service Bus queues. Azure Queue Storage provides excellent support for logging. You can activate logging capabilities and then track all actions that occur on your queue. Again, the maximum size of a message in Azure Queue Storage is 64 KB, and your queue can have a maximum size limit of 200 TB. By contrast, an Azure Service Bus message can be as large as 256 KB, and the queue can have a maximum size limit of 80 GB. Azure Queue Storage supports scheduling and batch processing, but unlike Azure Service Bus, Azure Queue Storage doesn’t support duplicate detection or state management, nor does it guarantee the FIFO order of messages. If you need to leverage transaction support, guarantee that messages are ordered, or guarantee that messages are unique, or you need to store your messages for a lengthy period of time, Azure Service Bus is the better choice. Install the Azure.Storage.Queues NuGet package To work with Azure Queue Storage, you should install the Azure.Storage.Queues NuGet package into your project. You can install this package from the NuGet package manager or by running the following command in the package manager console window. PM> Install-Package Azure.Storage.Queues Create an Azure Queue Storage client If you don’t already have an Azure Storage account, you should create one using the Azure Portal or Azure PowerShell or Azure CLI. Then copy the connection string and the account access keys because you’ll need them to connect to your Azure Storage account. Next, you should create an Azure Queue Storage client, which you can do by writing the following code: public static void CreateQueueClient(string queueName) { string connectionString = “Your Azure Storage Connection String”; QueueClient queueClient = new QueueClient(connectionString, queueName); } Send and receive messages using Azure Queue Storage The following code snippet illustrates how you can send messages using Azure Queue Storage. string connectionString = "Your Azure Storage Connection String"; string queueName = "test"; QueueClient queue = new QueueClient(connectionString, queueName); queue.Create(); queue.SendMessage("This is the first message."); queue.SendMessage("This is the second message."); foreach (QueueMessage message in queue.ReceiveMessages(maxMessages: 100).Value) { //Write your code here to process the messages queue.DeleteMessage(message.MessageId, message.PopReceipt); //Delete the message once it has been processed } Create a new queue in Azure Queue Storage The following code listing illustrates how you can create a new queue instance using the QueueClient class. public static bool CreateQueue(string queueName) { try { string connectionString = “Your Azure Storage Connection String”; QueueClient queueClient = new QueueClient (connectionString, queueName); queueClient.CreateIfNotExists(); if (queueClient.Exists()) { return true; } return false; } catch { return false; } } Add a message to a queue in Azure Queue Storage You can add a message to queue in Azure Queue Storage using the SendMessage method as shown in the code snippet given below. public static bool AddMessage(string queueName, string message) { try { string connectionString = "Your Azure Storage Connection String"; QueueClient queueClient = new QueueClient (connectionString, queueName); queueClient.CreateIfNotExists(); if (queueClient.Exists()) { queueClient.SendMessage(message); return true; } return false; } catch { return false; } } Peek at messages in Azure Queue Storage You can peek at one or more messages from the front of a queue in Azure Queue Storage using the PeekMessage or PeekMessages method. For example: public bool PeekMessage(string queueName) { try { string connectionString = "Your Azure Storage Connection String"; QueueClient queueClient = new QueueClient (connectionString, queueName); if (queueClient.Exists()) { PeekedMessage[] peekedMessage = queueClient.PeekMessage(); var messageBody = peekedMessage[0].Body; //Write your code here to work with the message body return true; } return false; } catch { return false; } } Update a message in Azure Queue Storage You can update a message, i.e., change the content of a message, using the UpdateMessage method of the QueueClient class as shown below. public static bool UpdateMessage(string queueName) { try { string connectionString = "Your Azure Storage Connection String"; QueueClient queueClient = new QueueClient (connectionString, queueName); if (queueClient.Exists()) { QueueMessage[] message = queueClient.ReceiveMessages(); queueClient.UpdateMessage(message[0].MessageId, "Edit the message"); return true; } return false; } catch { return false; } } Delete a queue in Azure Queue Storage To delete a queue, you can take advantage of the Delete method of the QueueClient class as shown in the code snippet given below. public static bool DeleteQueue(string queueName) { try { string connectionString = "Your Azure Storage Connection String"; QueueClient queueClient = new QueueClient (connectionString, queueName); if (queueClient.Exists()) { queueClient.Delete(); return true; } } catch { return false; } return false; } The Azure Storage Queues client library throws a RequestFailedException together with the relevant error codes on failure. This can help you in troubleshooting to trace the cause of the error. I’ll write more about using Azure Queue Storage in a future post here. Related content analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft rebrands Azure AI Studio to Azure AI Foundry The toolkit for building generative AI applications has been packaged with new updates to form the Azure AI Foundry service. By Anirban Ghoshal Nov 19, 2024 4 mins Microsoft Azure Generative AI Development Tools analysis Understanding Hyperlight, Microsoft’s minimal VM manager Microsoft is making its Rust-based, functions-focused VM tool available on Azure at last, ready to help event-driven applications at scale. By Simon Bisson Nov 14, 2024 8 mins Microsoft Azure Rust Serverless Computing analysis GitHub Copilot learns new tricks GitHub and Microsoft have taken their AI-powered programming assistant into new territories, tackling code reviews, simple web apps, Java upgrades, and Azure help and troubleshooting. By Simon Bisson Nov 07, 2024 8 mins GitHub Java Microsoft Azure Resources Videos