Joydip Kanjilal
Contributor

How to work with Azure Queue Storage in C#

how-to
Aug 05, 20217 mins
C#Cloud ComputingMicrosoft .NET

Take advantage of Azure Storage queues to store large numbers of messages for asynchronous or even distributed processing.

abstract data flows / data streams
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.

  1. Launch the Visual Studio IDE.
  2. Click on “Create new project.”
  3. In the “Create new project” window, select “Console App (.NET Core)” from the list of templates displayed.
  4. Click Next.
  5. In the “Configure your new project” window shown next, specify the name and location for the new project.
  6. 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.

Joydip Kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author