Azure Service Bus is an enterprise cloud messaging service for offline and online applications and services. Let’s go through an example of how to use this.
Log into the Azure Portal at https://portal.azure.com. Select + Enterprise Integration->Service Bus:
Enter details and click Create:
Select the Namespace created and then Shared Access Properties->RootManagerSharedAccessKey:
Copy the connection string primary key:
Also copy the primary key and paste this somewhere for later, e.g. notepad.
In the namespace, select queues:
Add queue:
Enter a queue and keep all the other properties the same. Click Create:
The queue is now ready to have messages sent to it. We will create a C# console app to do this. Open Visual Studio and create a new console app:
Open Manage NuGet packages:
Click Browse and type in Azure Service Bus. Select the package below and click Install:
Add a using:
using Microsoft.ServiceBus.Messaging;
Add the code below. The connection string will have the primary key. Specify queue1 created above:
var connectionString = ""; var queueName = "samplequeue"; var client = QueueClient.CreateFromConnectionString(connectionString, queueName); client.OnMessage(message => { Console.WriteLine(String.Format("Message body: {0}", message.GetBody<String>())); Console.WriteLine(String.Format("Message id: {0}", message.MessageId)); }); Console.ReadLine();
In Azure Portal, select the queue. You will see one message:
We will now receive the message from the queue.
Create a new console app and add the code below:
var connectionString = "";
var queueName = "samplequeue";
var client = QueueClient.CreateFromConnectionString(connectionString, queueName);
client.OnMessage(message =>
{
Console.WriteLine(String.Format("Message body: {0}", message.GetBody<String>()));
Console.WriteLine(String.Format("Message id: {0}", message.MessageId));
});
Console.ReadLine();
The console displays the message:
The message is now removed from the queue:
I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.
IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.
THANK YOU, AND LET'S KEEP LEARNING TOGETHER.
CARL