Azure Service Bus – how to code?

This blog with provide information on the library and how to code a listener and sender of an Azure Service Bus.

Azure Service Bus Queue client

In the below, we will high-light the most important details in using Azure Service Bus Queue client from a .NET perspective.

Microsoft.Azure.ServiceBus – NuGet package – is a .NET library that simplify the sending and receiving of messages via Azure Service Bus.

using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.ServiceBus;

The QueueClient class is the most important class in this library. As the name suggest, this is the client to interact with Azure Service Bus.

var queueClient = new QueueClient(TextAppConnectionString, "QueueName")

To send a message to a queue, it is highly recommended to use async method; Thus we will user await QueueClient.SendAsync function.

string message = "Hello";
var encodedMessage = new Message(Encoding.UTF8.GetBytes(message));
await queueClient.SendAsync(encodedMessage);

To receive a message, we must implement a message handler

queueClient.RegisterMessageHandler(MessageHandler, messageHandlerOptions);

When processing of the message in the queue is finished, remove the message in the queue by calling:

await queueClient.CompleteAsync(message.SystemProperties.LockToken);

To close a client’s connection use – await queueClient.CloseAsync();

await queueClient.CloseAsync();

Azure Service Bus Topics

One of the major difference in Azure Service Bus Queue vs Azure Service Bus Topic is, in the latter, all “subscribe” client will receive the message. This ideal for use case that there are more than 1 interested clients (receivers).

To communicate with Azure Service Bus topics, it will also use Microsoft.Azure.ServiceBus NuGet package.

TopicClient class will be used instead of QueueClient class. Likewise, it will use SubscriptionClass class to receive messages from a subscriber.

The three classes are all derived from MessagingEntityClient class.

For each subscription in the topic, when can apply a filter, refer to Topic filters and action page of Microsoft. This is out-of-scope of this post.

To send a message to the topic, the following is needed.

Define the following namespace.

using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.ServiceBus;

Create an instance of TopicClient class.

var topicClient = new TopicClient(TextAppConnectionString, "TopicName");

The TextAppConnectionString is the connection string from your SAS Policy.

Send the message in the topic using TopicClient.SendAsync. Same with Queue, the message must be encoded in UTF8.

string message = "Hello World!";
var encodedMessage = new Message(Encoding.UTF8.GetBytes(message));
await topicClient.SendAsync(encodedMessage);

To receive messages, we will use SubscriptionClient class. You should notice that it is has 3 parameters; connectiongstring, topic name, and a subscription name.

var subscriptionClient = new SubscriptionClient(ServiceBusConnectionString, "Topicname", "SubscriptionName");

Register a message handler. This is the function that will process the received message.

subscriptionClient.RegisterMessageHandler(MessageHandler, messageHandlerOptions);

When processing of the message is complete – call the SubscriptionClient.CompleteAsync to remove the message in the queue.

await subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);

Leave a comment