5 minute introduction to Message-Based Communication with Azure Service Bus

In this short blogpost we're diving into the world of message-based communication, particularly focusing on Azure Service Bus. If you've primarily worked with request-response communication models, you're in for a treat. Message-based communication can be an extremely powerful addition to your toolbox and Azure Service Bus makes it exceptionally straightforward.


What is Message-Based Communication?

At its core, message-based communication involves sending and receiving messages between applications, decoupling the sender and receiver. This model contrasts with traditional request-response models, where the sender waits for an immediate response.

The beauty of message-based systems lies in the flexibility, scalability and ability to connect applications in a loosely coupled manner, making them ideal for micro-service architectures.


What is Azure Service Bus?

Azure Service Bus is a fully managed enterprise message broker. It's a key component for developing decoupled applications within Azure, offering robust features that support complex messaging patterns like Queues, Topics and Subscriptions. 

Azure Service Bus offers a robust platform for developers to implement message-based communication in their applications. Its integration with Azure combined with the resiliency, security and fault tolerance features, makes it an excellent choice for building scalable and reliable applications.


Let's get started!

In this quick example we’ll be setting up a Queue in Azure Service Bus from scratch, create a .NET console application responsible for sending messages to the Queue (a sender) and another console application that consumes these messages (a processor).

1. Create a Service Bus Namespace
Start by navigating to the Azure Portal, and create a new Service Bus namespace. This namespace serves as a container for all your messaging components.

2. Create a Queue in this Namespace
Within your namespace, create a new queue. Queues are ideal for simple one-way communication scenarios. Write down the connectionstring (found in the 'Shared access policies' section) and the name of your Queue.

3. Create a new .NET console application
Ensure you have the Azure Service Bus package installed:

Install-Package Azure.Messaging.ServiceBus

Add these few lines to create a sender: 

var connectionString = "your_connection_string";
var queueName = "your_queue_name";

var client = new ServiceBusClient(connectionString);
var sender = serviceBusClient.CreateSender(queueName);

await sender.SendMessageAsync(
new ServiceBusMessage("Hello Queue!"));

 (This will hopefully send a new message to your Queue which can be confirmed in the Azure Portal.)

4. Create another .NET console application
Now we will create a processor to register our message handler:

var client = new ServiceBusClient(connectionString);
var processor = client.CreateProcessor(queueName, new ServiceBusProcessorOptions());

processor.ProcessMessageAsync += MessageHandler;
await processor.StartProcessingAsync();

And this message handler might look something like this:

async Task MessageHandler(ProcessMessageEventArgs args)
    return Task.CompletedTask;

5. Run your applications! ✅
Now you have one application that sends messages to the queue and another application that consumes these messages. #h5yr

To keep things short and straightforward in this blog post, we are focusing on queues and 1-to-1 message-based communication. I'm not covering topics and subscriptions which is another feature in Azure Service Bus that enables 1-to-Many communication setup useful in an event driven architecture. We also didn't cover error handlers which is probably something you would want in a real-world application.


That's it, that's all! 🏂

I told you this was going to be a short blog post, didn’t I? Hope you enjoyed this introduction and the brief nature of it. 

Cheers friends! ❤️