The azure service bus is a fully managed message integration broker. If two applications want to communicate with each other with less dependency, then the Azure service bus can used to send and receive the messages between them. It can provide your application decoupling and asynchronization with great flexibility as the two applications have less dependency on each other. Azure service message is reliable and durable as well. You can send a small text message or a complex message (object) to the message queue.
What we are going to cover.
- How to create a service bus and queue in Azure.
- How to send/receive messages from Azure service bus queue using C# code.
- How to run applications with this code.
How to create a service bus and queue in Azure (short intro as it can be viewed in Azure sites)
- Log in to the Azure portal and navigate to the service bus.
- Now click on the add button to create a namespace.
You can choose a resource group or create a new resource group. Add the namespace and the location at your convenience.
Now there is an option of Pricing Tier. For this application, I have used Basic for learning purposes.
And fill in some other details in the tab and create the namespace. It will take some time and now we are ready to create a Queue.
Now open the newly created service bus namespace and create the queue.
After creating the namespace and the queue we need to create the claims and policies for the service bus namespace. Create a shared access policy for the service bus namespace.
There are 3 types of claims in shared access policy and one can apply all three claims to one policy or combine any two. But as per the application needs, I suggest you create a policy based on the responsibilities.
Claims
- Managed: Queue creation and deletion can be done with this claim
- Send: Only Message sending can be done with this claim
- Listen: Only Message received can be done using this claim
How to send/receive messages from the Azure service bus queue using the C# code:
Generally, in ideal situations, one application (sender) is responsible for sending messages to the queue and another application (receiver) listens to the queue. If any message is available in the queue it reads the message from the queue and marks/removesit. Microservices applications are the best suitable solution for the service bus.
In the article, I have used .net core(2.1) and C# code to demonstrate the Azure service bus console application. I am using Visual Studio 2017. Only sending and receiving messages will be the part of this article, the creation of queue using C# code is not included.
There are 3 parts to the console application.
Entry point of the application (of course the main method in Program class).
Code for Program.cs
using System;
namespace AzureServiceBusApp
{
class Program
{
static void Main(string[] args)
{
Program program = new Program();
program.GetMessageFromQueue();
program.PutMessageInQueue();
}
public void PutMessageInQueue()
{
ServiceBusSender sender = new ServiceBusSender();
do
{
Console.WriteLine("Please type message to send in azure and press enter: ");
var strMessage = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(strMessage))
{
sender.SendMessage(strMessage);
Console.WriteLine("Message sent successfully");
}
else
{
Console.WriteLine("Empty Message. Please enter some text");
}
}
while (Console.ReadKey(true).Key != ConsoleKey.End);
}
public void GetMessageFromQueue()
{
ServiceBusReceiver receiver = new ServiceBusReceiver();
receiver.Listener();
}
}
}
The main method (entry point) calls two methods; e.g. GetMessageFromQueue() and PutMessageInQueue().
GetMessageFromQueue() method creates the ServiceBusReceiver object and invokes the Listener method.
PutMessageInQueue() creates the ServiceBusSender object and invokes the SendMessage method.

Join the conversation! Your thoughts help the community grow.