Saturday, October 21, 2023

Azure Service Bus With C#

 Azure Service Bus is a fully managed messaging service from Microsoft that allows applications to send messages to each other and receive messages reliably, without having to worry about the underlying infrastructure. This service is often used for scenarios where decoupled communication between applications is required, such as microservices architecture, or for enabling communication between cloud and on-premises systems.

This article will explore Azure Service Bus, what it does, and how it can be used in a C# application.

One of the key features of Azure Service Bus is its ability to support multiple messaging patterns, such as point-to-point messaging, publish/subscribe messaging, and request/reply messaging. This makes it an ideal solution for many use cases, such as enabling communication between microservices, sending and receiving messages between cloud and on-premises systems, and handling background jobs or workflows.

Difference Between an Azure Service Bus and a Queue?

Azure Service Bus provides two primary messaging patterns: queues and topics/subscriptions. Both queues and topics/subscriptions enable the decoupling of the sender and receiver, but they differ in how they handle message delivery.

A queue is a unidirectional channel that holds messages until the recipient retrieves them. When a message is sent to a queue, it is stored in the queue until a consumer actively retrieves it. The queue guarantees that messages will be delivered in the order they were received, and each message will be delivered to a single consumer.

On the other hand, a topic is a publish/subscribe channel that allows multiple subscribers to receive messages that are published to a single topic. Subscribers can filter the messages they receive based on message properties, and messages can be delivered to multiple subscribers at once. Messages sent to a topic are held in a subscription until they are actively consumed, but they are not deleted when a subscriber receives them.

The main difference between an Azure Service Bus queue and a topic is that a queue provides a one-to-one message delivery model, while a topic provides a one-to-many message delivery model. Additionally, topics support message filtering and subscription, while queues do not.

Getting Started with Azure Service Bus in C#

To start using Azure Service Bus in a C# application, you'll need to create a Service Bus namespace and set up an Azure Service Bus Queue or a topic/subscription. A namespace is a container that holds all the messaging entities, such as queues and topics, for a given solution.

Once you have created a namespace, you can use Microsoft.Azure.ServiceBus NuGet package to interact with the Service Bus from your C# application. This package provides a simple and convenient API for sending and receiving messages, as well as managing messaging entities.

Just like connecting to a database, you will need an Azure Service Bus connection string. You can see an example in the code below.

Send and receive a message

Message sending is performed using the ServiceBusSender. Receiving is performed using the ServiceBusReceiver.

string connectionString = "<connection_string>";
string queueName = "<queue_name>";
// since ServiceBusClient implements IAsyncDisposable we create it with "await using"
await using var client = new ServiceBusClient(connectionString);

// create the sender
ServiceBusSender sender = client.CreateSender(queueName);

// create a message that we can send. UTF-8 encoding is used when providing a string.
ServiceBusMessage message = new ServiceBusMessage("Hello world!");

// send the message
await sender.SendMessageAsync(message);

// create a receiver that we can use to receive the message
ServiceBusReceiver receiver = client.CreateReceiver(queueName);

// the received message is a different type as it contains some service set properties
ServiceBusReceivedMessage receivedMessage = await receiver.ReceiveMessageAsync();

// get the message body as a string
string body = receivedMessage.Body.ToString();
Console.WriteLine(body);


Reference: https://learn.microsoft.comhttps://www.c-sharpcorner.com/

0 comments:

Post a Comment