Tallan Blog

Tallan’s Experts Share Their Knowledge on Technology, Trends and Solutions to Business Challenges

Integrating Cloud Services: Azure Service Bus

What is Azure Service Bus?

Azure Service Bus is an integration message broker used as a reliable platform for asynchronous data transfer between different applications and services. Data is transferred using messages which can contain JSON, XML, or simply text. These messages are either transferred over queues or topics. In short, queues are used for point-to-point communication between one sender and one receiver, and topics are used in publisher/subscriber scenarios, with one sender and multiple, subscribing receivers. This Microsoft document provides a more detailed explanation of the Service Bus, Queues, and Topics, as well as some advanced features to solve more complex messaging problems.

This article will provide an example implementation of an Azure Service Bus connecting two .NET Applications.

Implementing Azure Service Bus

Azure Portal

               Setting up a Service Bus through the Azure portal is a very simple process, but you should be familiar with the different pricing tiers before moving forward. If you know you will only need to utilize queues and not topics, then consider sticking with the Basic tier. This demonstration will only go over queues, and therefore will use the Basic tier. This also assumes you already have an Azure Subscription and Resource Group set up.

               Once in the Azure Portal, click on Create a resource -> search for “Service Bus” -> and click Create. Fill in details regarding location, pricing, etc. and click Create. Once created, navigate to your new service bus -> Shared access policies and click the “RootManageSharedAccessKey”. Copy down the Primary Connection String for later.

Primary Connection String

To add a new queue, navigate to Queues and click “+ Queue”. You can then fill out the necessary properties for your queue. Please note that Duplicate Detection and Sessions are prohibited in the Basic pricing tier.

Azure Portal Queues

Visual Studio

In Visual Studio, the NuGet package you will need is Microsoft.Azure.ServiceBus, for which the latest version can be installed via the Package Manager Console with the following command:

Install-Package Microsoft.Azure.ServiceBus

This package will be used for establishing a connection to your service bus and provides basic methods used to interact with the queue(s) you set up. For simplicity, this example will simply send string messages between applications.

Send Message

               The send message method for our service bus will perform four actions:

  1. Open a connection through a queue client.
  2. Encode our string message as a Service Bus Message.
  3. Send the message.
  4. Close our connection.

See below for the implementation:

Send message

Remember to use your Primary Connection String from the Azure Portal for your serviceBusConnectionString. Leaving the queueName as a parameter allows calling functions to specify which queue to send the message through, therefore supporting multiple queues in a service bus instance.

Read Message

               The read message method for our service bus will also perform four actions:

  1. Open a connection through a message receiver.
  2. Retrieve the first message from the service bus.
  3. Close our connection.
  4. Decode our string message from the Service Bus Message.

See below for the implementation:

Read message

Important to note here is the ReceiveMode setting on line 26. This is set to ReceiveAndDelete, meaning that the message will be removed from the queue after being read. The other option is PeekLock, which won’t remove the message from the queue.

Demo

We can use a pair of console applications to test out our new service bus, both of which will reference our service bus class. The first will be a simple message sender program:

First Test

Running this allows us to type a message to send to the other app:

message

If we go back into the Azure Portal and select our queue, we can see that there is an active message waiting to be read:

Active message queue

Now it’s time to read the message. Our other console application will be a simple message reader program:

simple message reader

Running this allows us to read the message from the other app:

message 2

Now if we go back into the Azure Portal and select our queue, we can see that there are no active messages waiting to be read:

Active message queue 2

That’s it! We’ve implemented a fully functional Service Bus that can be used for asynchronous data transfer between different applications and services.


Learn more about Tallan or see us in person at one of our many Events!

Share this post:

No comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

\\\