Abstracting Azure Service Bus And Azure Queues To Ship Messages
Constructing trendy functions requires builders to always undertake newer applied sciences over time. Over the previous few years, a number of queuing applied sciences have been launched, with varied protocols and SDKs, which might make it tough for builders to rapidly undertake new applied sciences over time.
On this article, I’ll reveal easy methods to write a easy queuing interface that can be utilized to ship messages with the Azure Service Bus and Azure Queues utilizing C# designed to summary the primary supply code from the precise underlying queuing expertise in order that the code is less complicated to write down and simpler to improve over time when new applied sciences are launched. You’ll be able to leverage this code to increase it with different queuing applied sciences as properly (equivalent to MQ Collection and RabbitMQ), and add options to the underlying logic to assist extra superior capabilities of particular person platforms.
The Idea
To maintain issues easy, let’s construct an interface that may permit an software to ship a single message as a string (a JSON doc for instance). The pattern software will merely ship a message as a string utilizing a library that we are going to name QueueLib. This library may have two courses (AZBus and AZQueue) that may ship a message in both the Azure Bus or an Azure Queue. This can be carried out by means of an interface in order that the shopper code doesn’t have to know a lot in regards to the internals of the queuing platform getting used.
Observe that the power to decide on which class to make use of could be so simple as letting the shopper code make the choice, implementing some extra logic based mostly on configuration settings, and even implementing an Inversion of Management mechanism. On this article, I’m selecting so as to add easy logic that may decide which class to instantiate based mostly on the configuration settings of the applying.
In regards to the QueueLib Library
Let’s first create the QueueLib venture utilizing C# as a .NET library. This venture comprises an Interface that abstracts entry to the underlying objects and comprises a single technique signature: Ship, and two properties (ConnectionString and QueueName). Observe that on this instance, we’re not permitting message-level choices to simplify the code; you could possibly prolong this instance by permitting choices equivalent to a message TimeToLive parameter.
utilizing System;
namespace QueueLib
public interface IQueueService
void Ship(string payload);
string ConnectionString get; set;
string QueueName get; set;
Subsequent, we’ll implement the AZQueue class that implements the IQueueService interface. This class would require the usage of the Microsoft.Azure.Storage.Queue NuGet package deal. The category exposes two properties (ConnectionString and QueueName), together with the Ship technique.
utilizing System;
utilizing Microsoft.Azure.Storage;
utilizing Microsoft.Azure.Storage.Queue;
namespace QueueLib
public class AZQueue : IQueueService
public string ConnectionString get; set;
public string QueueName get; set;
public void Ship(string payload)
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
CloudQueue queue = queueClient.GetQueueReference(QueueName);
CloudQueueMessage message = new CloudQueueMessage(payload);
queue.AddMessage(message);
Lastly, let’s implement the AZBus class that additionally implements the IQueueService interface. This class would require the usage of Microsoft.Azure.ServiceBus NuGet package deal.
utilizing Microsoft.Azure.ServiceBus;
utilizing System.Textual content;
namespace QueueLib
public class AZBus : IQueueService
public string ConnectionString get; set;
public string QueueName get; set;
non-public static IQueueClient queueClient;
public void Ship(string payload)
queueClient = new QueueClient(ConnectionString, QueueName);
var message = new Message(Encoding.UTF8.GetBytes(payload));
queueClient.SendAsync(message).Wait();
Now that each courses and the interface have been created, let’s see the shopper software code.
Constructing the Shopper Console Utility
The shopper software can be utilizing the IQueueService interface to ship its messages; a way referred to as GetQueue() may have the logic vital to determine which queue to return (both AZBus or AZQueue). The important thing right here is that the logic to pick the proper queue object is abstracted from the primary code; on this case, the logic merely appears on the ConnectionString worth within the configuration file to make that willpower. Since each service may have a distinct format for its connection string it may be so simple as inspecting its content material to return the proper object. There are extra sturdy methods to implement this logic, however for a easy case equivalent to this one, it’s all we’d like. The app settings of the shopper code seem like this.
As soon as the article has been created, it’s merely a matter of calling the Ship() technique.
utilizing System;
utilizing QueueLib;
namespace ConsoleAppAZQueueDemo
class Program
static void Principal(string[] args)
var service = GetQueue();
service.Ship("check message");
Console.Learn();
static IQueueService GetQueue()
// Central logic that returns the queue to make use of
string connectionString = System.Configuration.ConfigurationManager.AppSettings["connectionString"];
string queueName = System.Configuration.ConfigurationManager.AppSettings["queueName"];
// Detect which sort of queue we have to create, based mostly on the connection string
IQueueService service = null;
if (connectionString.ToLower().Accommodates("core.home windows.internet"))
service = new AZQueue();
else if (connectionString.ToLower().Accommodates("sb://"))
service = new AZBus();
service.ConnectionString = connectionString;
service.QueueName = queueName;
return service;
Testing with the Azure Queue
If the connection string comprises core.home windows.internet, we all know it’s an Azure Storage Queue; within the screenshot under the message despatched could be seen within the Azure Portal straight.
Testing with the Service Bus Queue
If the connection string comprises sb://, we all know it’s an Azure Service Bus; the screenshot under reveals the message despatched to the Azure Bus Queue.
Conclusion
On this easy software, we will see the advantages of abstracting the queue service in a library that may do the onerous work for us. Altering the queue turns into so simple as altering the configuration settings of the applying.
There are numerous alternatives to enhance this easy code, equivalent to the power to obtain messages, implementing queue-specific choices, and a extra sturdy technique for selecting the queueing expertise to implement simply to call just a few.
Know extra about our firm at Skrots. Know extra about our providers at Skrots Providers, Additionally checkout all different blogs at Weblog at Skrots
Know more about our company at Skrots. Know more about our services at Skrots Services, Also checkout all other blogs at Blog at Skrots
Thanks, Harsh
Founder | CEO — Skrots
Learn more about our blog at Blog at Skrots. Checkout our list of services on Skrots. Give a look at our website design at Skrots . Checkout our LinkedIn Page at LinkedIn.com. Check out our original post at https://blog.skrots.com/abstracting-azure-service-bus-and-azure-queues-to-send-messages/?feed_id=5799&_unique_id=662070d02af25