A queue is a knowledge structure that works on a FIFO (to start with in to start with out) basis. Objects are inserted at the rear of the queue and taken out from the front. The expression “Enqueue” denotes the procedure that inserts knowledge in the queue, although the expression “Dequeue” denotes the removal of knowledge from the queue.

Azure supports two types of queues: the Azure Storage queues and Azure Services Bus queues. This short article discusses Azure Storage queues, how they differ from the Azure Services Bus queues, and how to get the job done with Azure Storage queues programmatically.

To get the job done with the code examples presented in this short article, you must have Visible Studio 2019 set up in your program. If you really don’t already have a duplicate, you can download Visible Studio 2019 in this article.

Make a console application undertaking in Visible Studio

First off, let’s generate a .Web Core console application undertaking in Visible Studio. Assuming Visible Studio 2019 is set up in your program, abide by the actions outlined down below to generate a new .Web Core console application undertaking in Visible Studio.

  1. Start the Visible Studio IDE.
  2. Simply click on “Create new undertaking.”
  3. In the “Create new project” window, select “Console Application (.Web Core)” from the checklist of templates shown.
  4. Simply click Following.
  5. In the “Configure your new project” window shown next, specify the name and spot for the new undertaking.
  6. Simply click Make.

This will generate a new .Web Core console application undertaking in Visible Studio 2019. We’ll use this undertaking to get the job done with Azure Storage queues in the subsequent sections of this short article.

What is Azure Queue Storage?

Azure Queue Storage is a Microsoft Azure cloud provider that enables you to store wide quantities of messages for processing. You can choose benefit of authenticated HTTP or HTTPS calls to obtain messages from everywhere in the globe in a secure manner. The utmost size of a queue information is sixty four KB. A queue in Azure Queue Storage can store massive quantities of messages (even thousands and thousands), up to a utmost storage ability of 200 TB.

Like your other Azure Storage knowledge objects, like blobs, information, and tables, your queues are stored in your Azure Storage account. The Azure Storage account offers you a distinctive namespace for your Azure Storage knowledge, which you can obtain more than HTTP or HTTPS from everywhere on the planet.

Azure Storage queues vs. Azure Services Bus queues

Azure Services Bus is a scalable information fabric created on Azure Storage that gives dependable messaging as an Azure cloud provider. You can use it for a few distinct types of messaging: provider relays among on-prem and cloud environments matters for 1-to-many publish/subscribe communications and queues. In this segment, we’ll illustration the variations among Azure Storage queues and Azure Services Bus queues.

Azure Queue Storage gives great help for logging. You can activate logging abilities and then monitor all steps that take place on your queue. Once again, the utmost size of a information in Azure Queue Storage is sixty four KB, and your queue can have a utmost size restrict of 200 TB. By contrast, an Azure Services Bus information can be as significant as 256 KB, and the queue can have a utmost size restrict of 80 GB.

Azure Queue Storage supports scheduling and batch processing, but unlike Azure Services Bus, Azure Queue Storage doesn’t help duplicate detection or condition administration, nor does it promise the FIFO get of messages. If you have to have to leverage transaction help, promise that messages are requested, or promise that messages are exceptional, or you have to have to store your messages for a prolonged period of time, Azure Services Bus is the far better selection.

Put in the Azure.Storage.Queues NuGet offer

To get the job done with Azure Queue Storage, you must put in the Azure.Storage.Queues NuGet offer into your undertaking. You can put in this offer from the NuGet offer supervisor or by managing the pursuing command in the offer supervisor console window.

PM> Put in-Deal Azure.Storage.Queues

Make an Azure Queue Storage customer

If you really don’t already have an Azure Storage account, you must generate 1 applying the Azure Portal or Azure PowerShell or Azure CLI. Then duplicate the relationship string and the account obtain keys for the reason that you will have to have them to hook up to your Azure Storage account.

Following, you must generate an Azure Queue Storage customer, which you can do by crafting the pursuing code:

general public static void CreateQueueClient(string queueName)

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

Send and get messages applying Azure Queue Storage

The pursuing code snippet illustrates how you can mail messages applying Azure Queue Storage.

string connectionString = "Your Azure Storage Connection String"
string queueName = "test"
QueueClient queue = new QueueClient(connectionString, queueName)
queue.Make()
queue.SendMessage("This is the to start with information.")
queue.SendMessage("This is the 2nd information.")
foreach (QueueMessage information in queue.ReceiveMessages(maxMessages: one hundred).Price)

    //Write your code in this article to system the messages
    queue.DeleteMessage(information.MessageId, information.PopReceipt)
    //Delete the information as soon as it has been processed

Make a new queue in Azure Queue Storage

The pursuing code listing illustrates how you can generate a new queue instance applying the QueueClient class.

general public static bool CreateQueue(string queueName)

        attempt
       
        string connectionString = “Your Azure Storage Connection String”
        QueueClient queueClient = new QueueClient
        (connectionString, queueName)
        queueClient.CreateIfNotExists()
        if (queueClient.Exists())
       
             return true
       
        return bogus
       
       capture
      
            return bogus
      

Include a information to a queue in Azure Queue Storage

You can add a information to queue in Azure Queue Storage applying the SendMessage approach as shown in the code snippet specified down below.

general public static bool AddMessage(string queueName, string information)

   attempt
  
       string connectionString = "Your Azure Storage Connection String"
       QueueClient queueClient = new QueueClient
       (connectionString, queueName)
       queueClient.CreateIfNotExists()
       if (queueClient.Exists())
      
           queueClient.SendMessage(information)
           return true
      
      return bogus
  
   capture
  
        return bogus
             

Peek at messages in Azure Queue Storage

You can peek at 1 or far more messages from the front of a queue in Azure Queue Storage applying the PeekMessage or PeekMessages approach. For illustration:

general public bool PeekMessage(string queueName)
  
          attempt
         
          string connectionString = "Your Azure Storage Connection String"
          QueueClient queueClient = new QueueClient
          (connectionString, queueName)
                if (queueClient.Exists())
               
                    PeekedMessage[] peekedMessage =
                    queueClient.PeekMessage()
                    var messageBody = peekedMessage[].Entire body
                    //Write your code in this article to get the job done with the information entire body
                    return true
               
                return bogus
           
            capture
           
                return bogus
                       
    

Update a information in Azure Queue Storage

You can update a information, i.e., modify the material of a information, applying the UpdateMessage approach of the QueueClient class as shown down below.

general public static bool UpdateMessage(string queueName)

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

  capture
 
       return bogus
            

Delete a queue in Azure Queue Storage

To delete a queue, you can choose benefit of the Delete approach of the QueueClient class as shown in the code snippet specified down below.

general public static bool DeleteQueue(string queueName)

attempt
  
      string connectionString = "Your Azure Storage Connection String"
      QueueClient queueClient = new QueueClient
      (connectionString, queueName)
      if (queueClient.Exists())
     
            queueClient.Delete()
            return true
     
 
  capture
 
      return bogus
 
 return bogus

The Azure Storage Queues customer library throws a RequestFailedException collectively with the applicable mistake codes on failure. This can aid you in troubleshooting to trace the bring about of the mistake. I’ll create far more about applying Azure Queue Storage in a long run publish in this article.

Copyright © 2021 IDG Communications, Inc.