Azure Message Queue Example

Introduction

Here in this blog, we will see the code examples to create an azure queue for the message in the azure storage queue and fetch that azure queue using .net core.

Getting Started

Azure Queue Storage is a message queuing services of Azure cloud to storage large numbers of messages in Azur storage blob. These messages can be accessed later from anywhere in the world via authenticated cal using HTTP or HTTPs. The queue message can be up to 64 KB in size and may contain millions of messages, but is limited to the capacity of a storage account.

The Azure storage queue provides various client libraries for different languages to create and fetch azure message queue from the azure storage queue. Here we will see the code example of how to create and fetch Azure message queue by using the .net client library in .net core.

Follow the below steps to create and remove from Azure message queue.

  1. Open Microsoft Visual Studio and create a new .net core console application project.

  2. Install the latest version of the following two libraries from NuGet Package Manager.

    1. Microsoft.Azure.Storage.Common:-

      This client library enables working with the Microsoft Azure Storage services which include the blob and file services for storing binary and text data, and the queue service for storing messages that may be accessed by a client.

    2. Microsoft.Azure.Storage.Queue:-

      This client library enables working with the Microsoft Azure Storage Queue service for storing messages that may be accessed by a client.

  3. I have installed the version 11.1.7 of both libraries.

  4. Make sure that you have already created the Azure message Queue Storage Account using Azure Portal.

  5. Open the Programm class and add below createMessage function in the program class.

     public static void CreateMessage(string connectionstring)  
         {  
           CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionstring);  
           CloudQueueClient cloudQueueClient = storageAccount.CreateCloudQueueClient();  
           CloudQueue cloudQueue = cloudQueueClient.GetQueueReference("MyTest_Messagequeue");  
           CloudQueueMessage queueMessage = new CloudQueueMessage("This is my first test message queue Created from Console Application");  
           cloudQueue.AddMessage(queueMessage);  
         }  
    
  6. The above function is to create an azure queue in Azure queue storage.

  7. Then copy the GetMessage code from below and add it to the program class.

     public static void GetMessage(string connectionstring)  
         {  
           CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionstring);  
           CloudQueueClient cloudQueueClient = storageAccount.CreateCloudQueueClient();  
           CloudQueue cloudQueue = cloudQueueClient.GetQueueReference("MyTest_Messagequeue");  
           CloudQueueMessage queueMessage = cloudQueue.GetMessage();  
           Console.WriteLine(queueMessage.AsString);  
         }  
    
  8. Go to the main method and declare a string variable for the message queue connection string.

  9. Set the value of the connection string variable to your valid connection string. Like the below example.

     //Avalide ConnectionString//  
           string messageQueueConnection= "DefaultEndpointsProtocol=https;AccountName= accountName;AccountKey=keyName;EndpointSuffix=core.windows.net";  
    
  10. Call the CreateMessage function and pass the connection string as a parameter to the function, later call the GetMessage function.

  11. Now run the project and see the result, if still you are facing problem then see the below full code example.

 using Microsoft.Azure.Storage;  
 using Microsoft.Azure.Storage.Queue;  
 using System;  
 namespace azurequeue  
 {  
   class Program  
   {  
     static void Main(string[] args)  
     {  
       Console.WriteLine("My First message queue programm!");  
       //Avalide ConnectionString//  
       string messageQueueConnection= "DefaultEndpointsProtocol=https;AccountName= accountName;AccountKey=keyName;EndpointSuffix=core.windows.net";  
       CreateMessage(messageQueueConnection);  
       GetMessage(messageQueueConnection);  
     }  
     public static void CreateMessage(string connectionstring)  
     {  
       CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionstring);  
       CloudQueueClient cloudQueueClient = storageAccount.CreateCloudQueueClient();  
       CloudQueue cloudQueue = cloudQueueClient.GetQueueReference("MyTest_Messagequeue");  
       CloudQueueMessage queueMessage = new CloudQueueMessage("This is my first test message queue Created from Console Application");  
       cloudQueue.AddMessage(queueMessage);  
     }  
     public static void GetMessage(string connectionstring)  
     {  
       CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionstring);  
       CloudQueueClient cloudQueueClient = storageAccount.CreateCloudQueueClient();  
       CloudQueue cloudQueue = cloudQueueClient.GetQueueReference("MyTest_Messagequeue");  
       CloudQueueMessage queueMessage = cloudQueue.GetMessage();  
       Console.WriteLine(queueMessage.AsString);  
     }  
   }  
 }  

Summary

In the above example, we saw how to create queues in azure and fetch azure queue message from azure queue storage, I hope you have enjoyed it a lot.

Thanks

Kailash Chandra Behera

An IT Professional with 12 years experience in development life cycle in windows, service and Web based application using Microsoft.Net technologies. Proven record of developing all phases of projects in Microsoft.Net technology from initiation to closure aligning with the company's Business objectives to drive process improvements, competitive advantage and bottom-line gains. -> Good exposure of independently working and developing multiple projects ->Committed to efficient and effective development of projects in a fast-paced and deadline driver environment. Skill :- Develop and design projects in various technologies of Microsoft Technology. Total IT Experience- 13+

Previous Post Next Post

نموذج الاتصال