How to work with Azure Queue Storage in C#

Nancy J. Delong

A queue is a data construction that is effective on a FIFO (1st in 1st out) foundation. Objects are inserted at the rear of the queue and eradicated from the front. The phrase “Enqueue” denotes the operation that inserts data in the queue, while the phrase “Dequeue” denotes the removal 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 operate with Azure Storage queues programmatically.

To operate with the code illustrations delivered in this short article, you should really have Visual Studio 2019 installed in your method. If you really do not now have a copy, you can obtain Visual Studio 2019 in this article.

Produce a console software task in Visual Studio

Initially off, let us generate a .Web Core console software task in Visual Studio. Assuming Visual Studio 2019 is installed in your method, stick to the measures outlined down below to generate a new .Web Core console software task in Visual Studio.

  1. Start the Visual Studio IDE.
  2. Click on on “Create new task.”
  3. In the “Create new project” window, pick out “Console App (.Web Core)” from the listing of templates shown.
  4. Click on Future.
  5. In the “Configure your new project” window demonstrated future, specify the identify and place for the new task.
  6. Click on Produce.

This will generate a new .Web Core console software task in Visual Studio 2019. We’ll use this task to operate 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 assistance that enables you to store broad numbers of messages for processing. You can take benefit of authenticated HTTP or HTTPS calls to obtain messages from anyplace in the globe in a protected way. The greatest sizing of a queue message is sixty four KB. A queue in Azure Queue Storage can store large numbers of messages (even hundreds of thousands), up to a greatest storage capability 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 gives you a distinctive namespace for your Azure Storage data, which you can obtain more than HTTP or HTTPS from anyplace on the entire world.

Azure Storage queues vs. Azure Service Bus queues

Azure Service Bus is a scalable message material crafted on Azure Storage that gives reputable messaging as an Azure cloud assistance. You can use it for a few distinct sorts of messaging: assistance relays between on-prem and cloud environments subject areas for a single-to-quite a few publish/subscribe communications and queues. In this segment, we’ll illustration the distinctions between Azure Storage queues and Azure Service Bus queues.

Azure Queue Storage gives outstanding assist for logging. You can activate logging abilities and then monitor all actions that take place on your queue. Once more, the greatest sizing of a message in Azure Queue Storage is sixty four KB, and your queue can have a greatest sizing restrict of two hundred TB. By contrast, an Azure Service Bus message can be as large as 256 KB, and the queue can have a greatest sizing restrict of eighty GB.

Azure Queue Storage supports scheduling and batch processing, but in contrast to Azure Service Bus, Azure Queue Storage does not assist copy detection or state administration, nor does it assurance the FIFO get of messages. If you will need to leverage transaction assist, assurance that messages are purchased, or assurance that messages are one of a kind, or you will need to store your messages for a prolonged period of time of time, Azure Service Bus is the superior preference.

Install the Azure.Storage.Queues NuGet offer

To operate with Azure Queue Storage, you should really install the Azure.Storage.Queues NuGet offer into your task. You can install this offer from the NuGet offer supervisor or by running the adhering to command in the offer supervisor console window.

PM> Install-Package deal Azure.Storage.Queues

Produce an Azure Queue Storage shopper

If you really do not now have an Azure Storage account, you should really generate a single applying the Azure Portal or Azure PowerShell or Azure CLI. Then copy the relationship string and the account obtain keys simply because you’ll will need them to connect to your Azure Storage account.

Future, you should really generate an Azure Queue Storage shopper, which you can do by composing the adhering to code:

community static void CreateQueueClient(string queueName)

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

Send out and get messages applying Azure Queue Storage

The adhering to code snippet illustrates how you can send out messages applying Azure Queue Storage.

string connectionString = "Your Azure Storage Relationship String"
string queueName = "exam"
QueueClient queue = new QueueClient(connectionString, queueName)
queue.Produce()
queue.SendMessage("This is the 1st message.")
queue.SendMessage("This is the second message.")
foreach (QueueMessage message in queue.ReceiveMessages(maxMessages: one hundred).Benefit)

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

Produce a new queue in Azure Queue Storage

The adhering to code listing illustrates how you can generate a new queue occasion applying the QueueClient class.

community static bool CreateQueue(string queueName)

        try
       
        string connectionString = “Your Azure Storage Relationship String”
        QueueClient queueClient = new QueueClient
        (connectionString, queueName)
        queueClient.CreateIfNotExists()
        if (queueClient.Exists())
       
             return genuine
       
        return wrong
       
       capture
      
            return wrong
      

Insert a message to a queue in Azure Queue Storage

You can include a message to queue in Azure Queue Storage applying the SendMessage technique as demonstrated in the code snippet offered down below.

community static bool AddMessage(string queueName, string message)

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

Peek at messages in Azure Queue Storage

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

community bool PeekMessage(string queueName)
  
          try
         
          string connectionString = "Your Azure Storage Relationship String"
          QueueClient queueClient = new QueueClient
          (connectionString, queueName)
                if (queueClient.Exists())
               
                    PeekedMessage[] peekedMessage =
                    queueClient.PeekMessage()
                    var messageBody = peekedMessage[].System
                    //Compose your code in this article to operate with the message body
                    return genuine
               
                return wrong
           
            capture
           
                return wrong
                       
    

Update a message in Azure Queue Storage

You can update a message, i.e., alter the information of a message, applying the UpdateMessage technique of the QueueClient class as demonstrated down below.

community 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 genuine
   
  return wrong

  capture
 
       return wrong
            

Delete a queue in Azure Queue Storage

To delete a queue, you can take benefit of the Delete technique of the QueueClient class as demonstrated in the code snippet offered down below.

community static bool DeleteQueue(string queueName)

try
  
      string connectionString = "Your Azure Storage Relationship String"
      QueueClient queueClient = new QueueClient
      (connectionString, queueName)
      if (queueClient.Exists())
     
            queueClient.Delete()
            return genuine
     
 
  capture
 
      return wrong
 
 return wrong

The Azure Storage Queues shopper library throws a RequestFailedException jointly with the applicable error codes on failure. This can aid you in troubleshooting to trace the lead to of the error. I’ll produce much more about applying Azure Queue Storage in a potential article in this article.

Copyright © 2021 IDG Communications, Inc.

Next Post

Highest-resolution measurements of asteroid surface temperatures ever obtained from Earth -- ScienceDaily

A near assessment of the millimeter-wavelength emissions from the asteroid Psyche, which NASA intends to visit in 2026, has made the 1st temperature map of the object, giving new insight into its floor homes. The conclusions, explained in a paper released in Planetary Science Journal (PSJ) on August 5, are […]