How to work with Azure Queue Storage in C#

A queue is a details structure that will work on a FIFO (initially in initially out) basis. Items are inserted at the rear of the queue and removed from the front. The term “Enqueue” denotes the procedure that inserts details in the queue, while the term “Dequeue” denotes the removing of details from the queue.

Azure supports two forms of queues: the Azure Storage queues and Azure Assistance Bus queues. This report discusses Azure Storage queues, how they vary from the Azure Assistance Bus queues, and how to do the job with Azure Storage queues programmatically.

To do the job with the code examples provided in this report, you must have Visible Studio 2019 put in in your technique. If you do not currently have a duplicate, you can download Visible Studio 2019 right here.

Build a console software project in Visible Studio

To start with off, let us create a .Web Core console software project in Visible Studio. Assuming Visible Studio 2019 is put in in your technique, follow the ways outlined under to create a new .Web Core console software project in Visible Studio.

  1. Launch the Visible Studio IDE.
  2. Click on “Create new project.”
  3. In the “Create new project” window, find “Console Application (.Web Core)” from the checklist of templates exhibited.
  4. Click Upcoming.
  5. In the “Configure your new project” window demonstrated upcoming, specify the identify and spot for the new project.
  6. Click Build.

This will create a new .Web Core console software project in Visible Studio 2019. We’ll use this project to do the job with Azure Storage queues in the subsequent sections of this report.

What is Azure Queue Storage?

Azure Queue Storage is a Microsoft Azure cloud assistance that permits you to store broad figures of messages for processing. You can get advantage of authenticated HTTP or HTTPS calls to obtain messages from wherever in the globe in a safe way. The utmost dimensions of a queue concept is 64 KB. A queue in Azure Queue Storage can store substantial figures of messages (even thousands and thousands), up to a utmost storage capacity of two hundred TB.

Like your other Azure Storage details objects, which includes blobs, information, and tables, your queues are stored in your Azure Storage account. The Azure Storage account offers you a distinct namespace for your Azure Storage details, which you can obtain in excess of HTTP or HTTPS from wherever on the planet.

Azure Storage queues vs. Azure Assistance Bus queues

Azure Assistance Bus is a scalable concept fabric created on Azure Storage that delivers trustworthy messaging as an Azure cloud assistance. You can use it for a few unique forms of messaging: assistance relays in between on-prem and cloud environments topics for just one-to-many publish/subscribe communications and queues. In this area, we’ll instance the differences in between Azure Storage queues and Azure Assistance Bus queues.

Azure Queue Storage delivers exceptional assist for logging. You can activate logging capabilities and then keep track of all steps that take place on your queue. All over again, the utmost dimensions of a concept in Azure Queue Storage is 64 KB, and your queue can have a utmost dimensions restrict of two hundred TB. By contrast, an Azure Assistance Bus concept can be as substantial as 256 KB, and the queue can have a utmost dimensions restrict of eighty GB.

Azure Queue Storage supports scheduling and batch processing, but as opposed to Azure Assistance Bus, Azure Queue Storage doesn’t assist duplicate detection or state administration, nor does it ensure the FIFO order of messages. If you need to have to leverage transaction assist, ensure that messages are ordered, or ensure that messages are exceptional, or you need to have to store your messages for a prolonged period of time of time, Azure Assistance Bus is the much better option.

Install the Azure.Storage.Queues NuGet bundle

To do the job with Azure Queue Storage, you must install the Azure.Storage.Queues NuGet bundle into your project. You can install this bundle from the NuGet bundle supervisor or by jogging the following command in the bundle supervisor console window.

PM> Install-Deal Azure.Storage.Queues

Build an Azure Queue Storage customer

If you do not currently have an Azure Storage account, you must create just one using the Azure Portal or Azure PowerShell or Azure CLI. Then duplicate the connection string and the account obtain keys due to the fact you will need to have them to link to your Azure Storage account.

Upcoming, you must create an Azure Queue Storage customer, which you can do by producing the following code:

public static void CreateQueueClient(string queueName)

    string connectionString = “Your Azure Storage Connection String”
    QueueClient queueClient = new QueueClient(connectionString, queueName)

Ship and acquire messages using Azure Queue Storage

The following code snippet illustrates how you can ship messages using Azure Queue Storage.

string connectionString = "Your Azure Storage Connection String"
string queueName = "exam"
QueueClient queue = new QueueClient(connectionString, queueName)
queue.Build()
queue.SendMessage("This is the initially concept.")
queue.SendMessage("This is the second concept.")
foreach (QueueMessage concept in queue.ReceiveMessages(maxMessages: 100).Worth)

    //Publish your code right here to approach the messages
    queue.DeleteMessage(concept.MessageId, concept.PopReceipt)
    //Delete the concept when it has been processed

Build a new queue in Azure Queue Storage

The following code listing illustrates how you can create a new queue occasion 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 accurate
       
        return bogus
       
       catch
      
            return bogus
      

Incorporate a concept to a queue in Azure Queue Storage

You can insert a concept to queue in Azure Queue Storage using the SendMessage method as demonstrated in the code snippet given under.

public static bool AddMessage(string queueName, string concept)

   try
  
       string connectionString = "Your Azure Storage Connection String"
       QueueClient queueClient = new QueueClient
       (connectionString, queueName)
       queueClient.CreateIfNotExists()
       if (queueClient.Exists())
      
           queueClient.SendMessage(concept)
           return accurate
      
      return bogus
  
   catch
  
        return bogus
             

Peek at messages in Azure Queue Storage

You can peek at just one or additional messages from the front of a queue in Azure Queue Storage using the PeekMessage or PeekMessages method. For instance:

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[].Body
                    //Publish your code right here to do the job with the concept physique
                    return accurate
               
                return bogus
           
            catch
           
                return bogus
                       
    

Update a concept in Azure Queue Storage

You can update a concept, i.e., alter the content material of a concept, using the UpdateMessage method of the QueueClient class as demonstrated under.

public static bool UpdateMessage(string queueName)

   try
  
   string connectionString = "Your Azure Storage Connection String"
   QueueClient queueClient = new QueueClient
   (connectionString, queueName)
   if (queueClient.Exists())
  
     QueueMessage[] concept = queueClient.ReceiveMessages()
     queueClient.UpdateMessage(concept[].MessageId, "Edit the concept")
     return accurate
   
  return bogus

  catch
 
       return bogus
            

Delete a queue in Azure Queue Storage

To delete a queue, you can get advantage of the Delete method of the QueueClient class as demonstrated in the code snippet given under.

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 accurate
     
 
  catch
 
      return bogus
 
 return bogus

The Azure Storage Queues customer library throws a RequestFailedException collectively with the suitable error codes on failure. This can aid you in troubleshooting to trace the cause of the error. I’ll generate additional about using Azure Queue Storage in a upcoming submit right here.

Copyright © 2021 IDG Communications, Inc.