Licensing Consultant

Not just any technology

How to work with Azure Queue Storage in C#

A queue is a data construction that is effective on a FIFO (to start with in to start with out) basis. Products are inserted at the rear of the queue and taken off from the entrance. The phrase “Enqueue” denotes the operation that inserts data in the queue, though the phrase “Dequeue” denotes the elimination of data from the queue.

Azure supports two sorts of queues: the Azure Storage queues and Azure Service Bus queues. This short article discusses Azure Storage queues, how they vary from the Azure Service Bus queues, and how to work with Azure Storage queues programmatically.

To work with the code examples supplied in this short article, you need to have Visual Studio 2019 put in in your method. If you don’t currently have a copy, you can down load Visual Studio 2019 in this article.

Develop a console application undertaking in Visual Studio

Very first off, let us build a .Web Core console application undertaking in Visual Studio. Assuming Visual Studio 2019 is put in in your method, follow the ways outlined beneath to build a new .Web Core console application undertaking in Visual Studio.

  1. Launch the Visual Studio IDE.
  2. Simply click on “Create new undertaking.”
  3. In the “Create new project” window, choose “Console Application (.Web Core)” from the listing of templates shown.
  4. Simply click Next.
  5. In the “Configure your new project” window revealed upcoming, specify the title and area for the new undertaking.
  6. Simply click Develop.

This will build a new .Web Core console application undertaking in Visual Studio 2019. We’ll use this undertaking to work 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 service that enables you to keep broad numbers of messages for processing. You can get advantage of authenticated HTTP or HTTPS calls to obtain messages from wherever in the world in a protected manner. The most sizing of a queue message is sixty four KB. A queue in Azure Queue Storage can keep massive numbers of messages (even millions), up to a most storage ability of two hundred TB.

Like your other Azure Storage data objects, like blobs, data files, and tables, your queues are stored in your Azure Storage account. The Azure Storage account presents you a unique namespace for your Azure Storage data, which you can obtain in excess of HTTP or HTTPS from wherever on the entire world.

Azure Storage queues vs. Azure Service Bus queues

Azure Service Bus is a scalable message cloth built on Azure Storage that provides trustworthy messaging as an Azure cloud service. You can use it for 3 distinctive sorts of messaging: service relays in between on-prem and cloud environments subject areas for one particular-to-several publish/subscribe communications and queues. In this part, we’ll instance the variations in between Azure Storage queues and Azure Service Bus queues.

Azure Queue Storage provides outstanding guidance for logging. You can activate logging capabilities and then keep track of all actions that arise on your queue. Yet again, the most sizing of a message in Azure Queue Storage is sixty four KB, and your queue can have a most sizing limit of two hundred TB. By distinction, an Azure Service Bus message can be as substantial as 256 KB, and the queue can have a most sizing limit of eighty GB.

Azure Queue Storage supports scheduling and batch processing, but in contrast to Azure Service Bus, Azure Queue Storage does not guidance replicate detection or condition administration, nor does it assure the FIFO order of messages. If you will need to leverage transaction guidance, assure that messages are requested, or assure that messages are distinctive, or you will need to keep your messages for a prolonged period of time of time, Azure Service Bus is the better selection.

Install the Azure.Storage.Queues NuGet package deal

To work with Azure Queue Storage, you need to put in the Azure.Storage.Queues NuGet package deal into your undertaking. You can put in this package deal from the NuGet package deal manager or by jogging the next command in the package deal manager console window.

PM> Install-Offer Azure.Storage.Queues

Develop an Azure Queue Storage client

If you don’t currently have an Azure Storage account, you need to build one particular working with the Azure Portal or Azure PowerShell or Azure CLI. Then copy the link string and the account obtain keys since you will will need them to link to your Azure Storage account.

Next, you need to build an Azure Queue Storage client, which you can do by creating the next code:

general public static void CreateQueueClient(string queueName)

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

Mail and receive messages working with Azure Queue Storage

The next code snippet illustrates how you can send out messages working with Azure Queue Storage.

string connectionString = "Your Azure Storage Relationship String"
string queueName = "test"
QueueClient queue = new QueueClient(connectionString, queueName)
queue.Develop()
queue.SendMessage("This is the to start with message.")
queue.SendMessage("This is the second message.")
foreach (QueueMessage message in queue.ReceiveMessages(maxMessages: 100).Benefit)

    //Produce your code in this article to method the messages
    queue.DeleteMessage(message.MessageId, message.PopReceipt)
    //Delete the message once it has been processed

Develop a new queue in Azure Queue Storage

The next code listing illustrates how you can build a new queue instance working with the QueueClient course.

general public static bool CreateQueue(string queueName)

        test
       
        string connectionString = “Your Azure Storage Relationship String”
        QueueClient queueClient = new QueueClient
        (connectionString, queueName)
        queueClient.CreateIfNotExists()
        if (queueClient.Exists())
       
             return accurate
       
        return untrue
       
       capture
      
            return untrue
      

Incorporate a message to a queue in Azure Queue Storage

You can increase a message to queue in Azure Queue Storage working with the SendMessage system as revealed in the code snippet supplied beneath.

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

   test
  
       string connectionString = "Your Azure Storage Relationship String"
       QueueClient queueClient = new QueueClient
       (connectionString, queueName)
       queueClient.CreateIfNotExists()
       if (queueClient.Exists())
      
           queueClient.SendMessage(message)
           return accurate
      
      return untrue
  
   capture
  
        return untrue
             

Peek at messages in Azure Queue Storage

You can peek at one particular or a lot more messages from the entrance of a queue in Azure Queue Storage working with the PeekMessage or PeekMessages system. For instance:

general public bool PeekMessage(string queueName)
  
          test
         
          string connectionString = "Your Azure Storage Relationship String"
          QueueClient queueClient = new QueueClient
          (connectionString, queueName)
                if (queueClient.Exists())
               
                    PeekedMessage[] peekedMessage =
                    queueClient.PeekMessage()
                    var messageBody = peekedMessage[].Overall body
                    //Produce your code in this article to work with the message system
                    return accurate
               
                return untrue
           
            capture
           
                return untrue
                       
    

Update a message in Azure Queue Storage

You can update a message, i.e., change the articles of a message, working with the UpdateMessage system of the QueueClient course as revealed beneath.

general public static bool UpdateMessage(string queueName)

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

  capture
 
       return untrue
            

Delete a queue in Azure Queue Storage

To delete a queue, you can get advantage of the Delete system of the QueueClient course as revealed in the code snippet supplied beneath.

general public static bool DeleteQueue(string queueName)

test
  
      string connectionString = "Your Azure Storage Relationship String"
      QueueClient queueClient = new QueueClient
      (connectionString, queueName)
      if (queueClient.Exists())
     
            queueClient.Delete()
            return accurate
     
 
  capture
 
      return untrue
 
 return untrue

The Azure Storage Queues client library throws a RequestFailedException together with the appropriate error codes on failure. This can assistance you in troubleshooting to trace the trigger of the error. I’ll generate a lot more about working with Azure Queue Storage in a long term write-up in this article.

Copyright © 2021 IDG Communications, Inc.