Azure Service Bus is a cloud-based messaging service provider provided by Microsoft Azure. Azure Service Bus provides a variety of messaging patterns, like queues, topics, and subscriptions. In this blog, we are going to see in detail how to send and receive messages using queues.
Prerequisites
- Azure portal Account and Subscription
- Azure Service Bus
- Visual Studio
- Azure functions
Queues
Queues are a fundamental messaging pattern used for asynchronous communication. It follows the first-in, first-out Out(FIFO) pattern. That is, receivers receive and process messages in the order in which they are added to the queue.
Send messages to queue
In C#, we can send a message to a queue using Azure.ServiceBus library. First, we need to install Azure.ServiceBus library using Package Manager Console.
- Open the Package Manager console and install this library using the below command.
Install-Package Microsoft.Azure.ServiceBus
2. Create a class ServiceBusQueueDetails for getting the values for creating queues.
namespace Practice.Models.Azure
{
public class ServiceBusQueueDetails
{
public string ConnectionString { get; set; }
public string QueueName { get; set; }
public string Message { get; set; }
}
}
ConnectionString – It is a connection string that contains the necessary authentication and configuration information to connect to your Azure Service Bus namespace. It should include information like the Service Bus endpoint URL, access key, and other settings required for authentication and communication.
QueueName: This is the name of the Azure Service Bus queue you want to send or receive messages from.
3. Create a Http action to send a message to the queue.
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.ServiceBus;
using Practice.Models.Azure;
using System.Text;
namespace Practice.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class QueueController : ControllerBase
{
static IQueueClient queueClient;
[HttpPost]
[Route("SendMessageToQueue")]
public async void SendMessageToQueue(ServiceBusQueueDetails S)
{
//Create queue with Connection string and Queue Name
queueClient = new QueueClient(S.ConnectionString, S.QueueName);
//Send message using queue client
await SendMessagesAsync(S.Message);
await queueClient.CloseAsync();
}
static async Task SendMessagesAsync(string Message)
{
try
{
var message = new Message(Encoding.UTF8.GetBytes(Message));
// Send the message to the queue
await queueClient.SendAsync(message);
Console.WriteLine($"Sent message: {Message}");
}
catch (Exception exception)
{
Console.WriteLine($"{DateTime.Now} :: Exception: {exception.Message}");
}
}
}
}
The code queueClient = new QueueClient(S.ConnectionString, S.QueueName) creates a queueClient instance for interacting with a specific Azure Service Bus queue using the connecting string and queue name.
And await queueClient.SendAsync(message) line is used to send message to the queue.
4. Create new Azure function project in Visual Studio for receiving messages from queue that has been sent in previous function. In local.settings file, configure values for ConnectionString of Service bus.
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
namespace Queue
{
public class Function
{
[FunctionName("MessageReceiverFunctiob")]
public void Run([ServiceBusTrigger("account-history", Connection = "ServiceBusConnectionString")]string myQueueItem, ILogger log)
{
log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
}
}
}
4. Run both the projects and give the connection string and queue name for which the message to be sent as request and execute this method. By clicking execute , message will be sent to the queue.
After execution of this method,SendMessageToQueue api will be called where the queue is created and message will be sent and received in the azure functions.
Conclusion
In thig blog, we discussed about sending and receiving messages in queue.
No Comment! Be the first one.