How to work with Azure Queue Storage in C#

Maria J. Danford

A queue is a details structure that will work on a FIFO (very first in very first out) foundation. Merchandise are inserted at the rear of the queue and eradicated from the entrance. The term “Enqueue” denotes the operation that inserts details in the queue, whilst the term “Dequeue” denotes the elimination of details 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 vary from the Azure Services Bus queues, and how to function with Azure Storage queues programmatically.

To function with the code examples presented in this short article, you must have Visible Studio 2019 installed in your method. If you really do not already have a duplicate, you can down load Visible Studio 2019 right here.

Develop a console software project in Visible Studio

To start with off, let’s generate a .Web Core console software project in Visible Studio. Assuming Visible Studio 2019 is installed in your method, abide by the steps outlined under to generate a new .Web Core console software project in Visible Studio.

  1. Start the Visible Studio IDE.
  2. Simply click on “Create new project.”
  3. In the “Create new project” window, select “Console Application (.Web Core)” from the checklist of templates shown.
  4. Simply click Up coming.
  5. In the “Configure your new project” window revealed subsequent, specify the identify and spot for the new project.
  6. Simply click Develop.

This will generate a new .Web Core console software project in Visible Studio 2019. We’ll use this project to function 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 company that permits you to retail outlet wide numbers of messages for processing. You can acquire advantage of authenticated HTTP or HTTPS calls to access messages from everywhere in the globe in a protected way. The most dimensions of a queue information is sixty four KB. A queue in Azure Queue Storage can retail outlet large numbers of messages (even millions), up to a most storage ability of two hundred TB.

Like your other Azure Storage details objects, together with blobs, information, and tables, your queues are stored in your Azure Storage account. The Azure Storage account provides you a unique namespace for your Azure Storage details, which you can access above HTTP or HTTPS from everywhere on the earth.

Azure Storage queues vs. Azure Services Bus queues

Azure Services Bus is a scalable information cloth constructed on Azure Storage that supplies responsible messaging as an Azure cloud company. You can use it for 3 different types of messaging: company relays concerning on-prem and cloud environments topics for one particular-to-a lot of publish/subscribe communications and queues. In this portion, we’ll instance the variations concerning Azure Storage queues and Azure Services Bus queues.

Azure Queue Storage supplies superb assistance for logging. You can activate logging capabilities and then monitor all steps that take place on your queue. All over again, the most dimensions of a information in Azure Queue Storage is sixty four KB, and your queue can have a most dimensions restrict of two hundred TB. By distinction, an Azure Services Bus information can be as significant as 256 KB, and the queue can have a most dimensions restrict of 80 GB.

Azure Queue Storage supports scheduling and batch processing, but as opposed to Azure Services Bus, Azure Queue Storage does not assistance copy detection or state management, nor does it assure the FIFO purchase of messages. If you require to leverage transaction assistance, assure that messages are ordered, or assure that messages are one of a kind, or you require to retail outlet your messages for a lengthy period of time of time, Azure Services Bus is the far better option.

Put in the Azure.Storage.Queues NuGet offer

To function with Azure Queue Storage, you must put in the Azure.Storage.Queues NuGet offer into your project. You can put in this offer from the NuGet offer manager or by managing the subsequent command in the offer manager console window.

PM> Put in-Offer Azure.Storage.Queues

Develop an Azure Queue Storage customer

If you really do not already have an Azure Storage account, you must generate one particular utilizing the Azure Portal or Azure PowerShell or Azure CLI. Then duplicate the connection string and the account access keys due to the fact you are going to require them to link to your Azure Storage account.

Up coming, you must generate an Azure Queue Storage customer, which you can do by producing the subsequent code:

community static void CreateQueueClient(string queueName)

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

Mail and receive messages utilizing Azure Queue Storage

The subsequent code snippet illustrates how you can ship messages utilizing Azure Queue Storage.

string connectionString = "Your Azure Storage Link String"
string queueName = "take a look at"
QueueClient queue = new QueueClient(connectionString, queueName)
queue.Develop()
queue.SendMessage("This is the very first information.")
queue.SendMessage("This is the next information.")
foreach (QueueMessage information in queue.ReceiveMessages(maxMessages: a hundred).Worth)

    //Compose your code right here to process the messages
    queue.DeleteMessage(information.MessageId, information.PopReceipt)
    //Delete the information after it has been processed

Develop a new queue in Azure Queue Storage

The subsequent code listing illustrates how you can generate a new queue occasion utilizing the QueueClient class.

community static bool CreateQueue(string queueName)

        try
       
        string connectionString = “Your Azure Storage Link String”
        QueueClient queueClient = new QueueClient
        (connectionString, queueName)
        queueClient.CreateIfNotExists()
        if (queueClient.Exists())
       
             return correct
       
        return phony
       
       catch
      
            return phony
      

Insert a information to a queue in Azure Queue Storage

You can insert a information to queue in Azure Queue Storage utilizing the SendMessage process as revealed in the code snippet provided under.

community static bool AddMessage(string queueName, string information)

   try
  
       string connectionString = "Your Azure Storage Link String"
       QueueClient queueClient = new QueueClient
       (connectionString, queueName)
       queueClient.CreateIfNotExists()
       if (queueClient.Exists())
      
           queueClient.SendMessage(information)
           return correct
      
      return phony
  
   catch
  
        return phony
             

Peek at messages in Azure Queue Storage

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

community bool PeekMessage(string queueName)
  
          try
         
          string connectionString = "Your Azure Storage Link String"
          QueueClient queueClient = new QueueClient
          (connectionString, queueName)
                if (queueClient.Exists())
               
                    PeekedMessage[] peekedMessage =
                    queueClient.PeekMessage()
                    var messageBody = peekedMessage[].Physique
                    //Compose your code right here to function with the information body
                    return correct
               
                return phony
           
            catch
           
                return phony
                       
    

Update a information in Azure Queue Storage

You can update a information, i.e., modify the articles of a information, utilizing the UpdateMessage process of the QueueClient class as revealed under.

community static bool UpdateMessage(string queueName)

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

  catch
 
       return phony
            

Delete a queue in Azure Queue Storage

To delete a queue, you can acquire advantage of the Delete process of the QueueClient class as revealed in the code snippet provided under.

community static bool DeleteQueue(string queueName)

try
  
      string connectionString = "Your Azure Storage Link String"
      QueueClient queueClient = new QueueClient
      (connectionString, queueName)
      if (queueClient.Exists())
     
            queueClient.Delete()
            return correct
     
 
  catch
 
      return phony
 
 return phony

The Azure Storage Queues customer library throws a RequestFailedException together with the suitable error codes on failure. This can enable you in troubleshooting to trace the induce of the error. I’ll compose more about utilizing Azure Queue Storage in a foreseeable future post right here.

Copyright © 2021 IDG Communications, Inc.

Next Post

He-Man Gets a New Twist in ‘Masters of the Universe: Revelation’

The new Netflix sequence Masters of the Universe: Revelation, created by Kevin Smith, is the newest offering from Powerhouse Animation, which also made the Netflix displays Blood of Zeus and Castlevania. Science fiction creator Zach Chapman thinks it’s top-quality to its predecessors. “I imagine the animation actually surpasses Blood of […]

Subscribe US Now