Azure Service Bus is a messaging infrastructure that sits between applications allowing them to exchange messages for improved scale and resiliency. It offers the following capabilities:
Queues: offers simple first in, first out guaranteed message delivery.
Queues: offers simple first in, first out guaranteed message delivery.
When to use Azure Service Bus?
Service Bus queues are a general-purpose technology that can be used for a wide variety of scenarios:
- Communication between web and worker roles in a multi-tier Azure application
- Communication between on-premises apps and Azure hosted apps in a hybrid solution
- Communication between components of a distributed application running on-premises in different organizations or departments of an organization
There are also Topics and Relay capabiliteis but we will talk about them in the future.
A figure that illustrates how Azure Service Bus works.


There is a sender who send a message with some properties to the queue and then the queue may do some processing over the message and send it to a receiver.
public class Program
{
public static string SericeBusConnectionString = "service_bus_connection_string";
public static void Main(string[] args)
{
string queueName = "queue_name";
var namespaceManager = NamespaceManager.CreateFromConnectionString(SericeBusConnectionString);
if (!namespaceManager.QueueExists(queueName))
{
namespaceManager.CreateQueue(queueName);
}
QueueClient client = QueueClient.CreateFromConnectionString(SericeBusConnectionString, queueName);
var message = new BrokeredMessage();
message.Properties["first_name"] = "Test First Name";
message.Properties["last_name"] = "Test Last Name";
client.SendAsync(message);
}
}
Don't forget to replace the connection string in the code with your service bus connection string and you replace queue name with anything that can describe what does your queue do.
We can now send message to the service bus but we don't know when will a message be sent, It could be sent at any moment so how can we handle this?
Easy, we can handle that by using Azure WebJob, A webjob is a background service that can either be always running or run on schedule, but in the case of service bus, we will create one which will be running continuously because we never know when will a message be sent.
Let's create a console application that will be uploaded to azure as a webjob writing the code that will receive the service message bus.
class Program
{
static void Main(string[] args)
{
string queueName = "queue_name";
var _servicesBusConn = "servie_bus_connection_string";
QueueClient client = QueueClient.CreateFromConnectionString(_servicesBusConn, queueName);
while (true)
{
var msg = client.Receive();
if (msg != null)
{
string fn = msg.Properties["first_name"].ToString();
string ln = msg.Properties["last_name"].ToString();
string name = fn + " " + ln;
Console.WriteLine("Name = " + name);
Trace.TraceInformation("Name = " + name);
msg.Complete();
}
}
}
}
}
Now, we need to create a zip file for the project binaries to upload as a webjob, It's pretty easy to upload a webjob all what you need to do is uploading and choose run continuously.
Congratulations, You have already created a service bus queue and handled everything you need to.
Here is the sample of sender code and the receiver code.
Azure Service Bus Vs Azure Web Job
Reviewed by Ashwani
on
March 25, 2020
Rating: 5
No comments:
Post a Comment