What is Azure Queue Storage?
Azure Queue Storage is a service for storing large numbers of messages. You access messages from anywhere in the world via authenticated calls using HTTP or HTTPS. A queue message can be up to 64 KB in size. A queue may contain millions of messages, up to the total capacity limit of a storage account. Queues are commonly used to create a backlog of work to process asynchronously.
Queue Storage Concepts
Azure Queue Storage implements cloud-based queues to enable communication between components of a distributed application. Each queue maintains a list of messages that can be added by a sender component and processed by a receiver component. Here are the components of the Queue Storage:
- URL format: Queues are addressable using the following URL format:
- Storage account: All access to Azure Storage is done through a storage account.
- Queue: A queue contains a set of messages. The queue name must be all lowercase.
- Message: A message, in any format, of up to 64 KB. The maximum time-to-live allowed is seven days it can be modified to any positive number, or -1 indicating that the message doesn't expire. If this parameter is omitted, the default time-to-live is seven days.
How to Access the Queue Storage using .NET
In the following steps, we are going to build a .NET console application to read IoT messages stored in the Queue Storage through Azure IoT Hub. For this tutorial, we are going to use the Visual Studio 2022 community version and the application will be created using .NET 6.
Step 1: Creating a .NET 6 Console Application
- Open VS2022, then from the top menu click on File
- From the menu, select New then Project
- Select Console App from the project templates, as per the following screenshot:
- Use "ReadFromAzureQueue" as the project name, then click Next to create the project. Make sure you select .NET6 which is the version used in this tutorial.
Here is a screenshot showing what the final outcome should look like:
Step 2: Installing Package “Azure.Storage.Queues”
- Open NuGet package manager
- Search online for package "Azure.Storage.Queues". We will install version "12.8.0".
- Click install to install the package and its dependencies. After that, you should be able to see the installed package under packages in the console application. The following screenshot showing the package installed under packages in our console application:
Step 3: Retrieve the ConnectionString from Azure
- Login to the Azure Portal
- Retrieve the storage account
- In the Settings section of the storage account overview, select Access keys. The account access keys appear, as well as the complete connection string for each key.
- Find the Connection string value under key1, and click the Copy button to copy the connection string. We will use this connection string to connect to the Queue in the next step.
Step 4: Retrieving Message from Azure Queue using .NET API
static async Task Main(string[] args)
{
string connectionString = "Put Here The Connection String We Copied In Step 3";
QueueClient queue = new QueueClient(connectionString, "UseTheQueueNameHere");
string value = await RetrieveNextMessageAsync(queue);
Console.WriteLine($"Received: {value}");
Console.Write("Press Enter...");
Console.ReadLine();
}
static async Task<string> RetrieveNextMessageAsync(QueueClient theQueue)
{
if (await theQueue.ExistsAsync())
{
QueueProperties properties = await theQueue.GetPropertiesAsync();
if (properties.ApproximateMessagesCount > 0)
{
QueueMessage[] retrievedMessage = await theQueue.ReceiveMessagesAsync(1);
string encodedString = retrievedMessage[0].Body.ToString();
byte[] data = Convert.FromBase64String(encodedString);
string decodedString = Encoding.UTF8.GetString(data);
return decodedString;
}
return string.Empty;
}
return string.Empty;
}