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.
Understanding Azure Message Queue
Getting Started
In today’s expanding world of distributed systems and microservices, ensuring effective communication between components can be complex. Microsoft Azure addresses this challenge with robust messaging solutions like Azure Queue Storage and Azure Service Bus. Known collectively as Azure Message Queue services, these tools empower developers to build scalable, decoupled, and resilient cloud-based applications.
Opt for Azure Queue Storage when you need a simple, cost-effective messaging solution with minimal configuration. Choose Azure Service Bus when your application requires advanced features like message ordering, reliability, and rich messaging capabilities.
What is Azure Message Queue?
Azure Message Queue refers broadly to Microsoft Azure's message queuing services designed for asynchronous communication between application components. These services allow for decoupling between the sender and receiver, ensuring that the application remains responsive and scalable, even under high load.
Azure provides two primary messaging services:- Azure Queue Storage – a simple message queue for basic communication needs.
- Azure Service Bus – a more robust enterprise-grade message broker supporting complex messaging patterns.
Here we will explore the Azure Queue Storage, see the code example to add and remove the message from. In the another post, we will explore the Azure Service Bus
.Azure Queue Storage?
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.
Azure Queue Storage offers client libraries for various programming languages, enabling developers to create and retrieve messages from queues with ease. In this example, we’ll demonstrate how to create and fetch messages from an Azure Storage Queue using the .NET
client library in a .NET Core
application.
Follow the below steps to create and remove from Azure message queue.
- Open Microsoft Visual Studio and create a new .net core console application project.
- Install the latest version of the following two libraries from NuGet Package Manager.
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.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.
- I have installed the version 11.1.7 of both libraries.
- Make sure that you have already created the Azure message Queue Storage Account using Azure Portal.
- Open the
Programm.cs
file 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); }
- The above function is to create an azure queue in Azure queue storage.
- 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); }
- Go to the main method and declare a string variable for the message queue connection string.
- 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";
- Call the CreateMessage function and pass the connection string as a parameter to the function, later call the GetMessage function.
- 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
Azure Queue Storage is a powerful yet straightforward tool for implementing reliable, scalable, and asynchronous messaging in the cloud. Whether you're building cloud-native apps, scaling out microservices, or managing background workflows, queues can help streamline your architecture.
Thanks