Abstracting Azure Service Bus And Azure Queues To Send Messages

In this article, you will learn about Abstracting Azure Service Bus and Azure Queues to Send Messages.

Building modern applications requires developers to constantly adopt newer technologies over time. Over the last few years, multiple queuing technologies have been introduced, with various protocols and SDKs, which can make it difficult for developers to quickly adopt new technologies over time.
 
In this article I will demonstrate how to write a simple queuing interface that can be used to send messages with the Azure Service Bus and Azure Queues using C# designed to abstract the main source code from the actual underlying queuing technology, so that the code is easier to write, and easier to upgrade over time when new technologies are introduced. You can leverage this code to extend it with other queuing technologies as well (such as MQ Series and RabbitMQ), and add features to the underlying logic to support more advanced capabilities of individual platforms.
 

The Concept

 
To keep things simple, let’s build an interface that will allow an application to send a single message as a string (a JSON document for example). The sample application will simply send a message as a string using a library that we will call QueueLib. This library will have two classes (AZBus and AZQueue) that can send a message in either the Azure Bus or an Azure Queue. This will be done through an interface so that the client code doesn’t need to know much about the internals of the queuing platform being used.
 
Abstracting Azure Service Bus And Azure Queues To Send Messages
 
Note that the ability to choose which class to use can be as simple as letting the client code make the decision, implementing some additional logic based on configuration settings, or even implement an Inversion of Control mechanism. In this article, I am choosing to add simple logic that will determine which class to instantiate based on the configuration settings of the application.
 

About the QueueLib Library

 
Let’s first create the QueueLib project using C# as a .NET library. This project contains an Interface that abstracts access to the underlying objects and contains a single method signature: Send, and two properties (ConnectionString and QueueName). Note that in this example, we are not allowing message-level options to simplify the code; you could extend this example by allowing options such as a message TimeToLive parameter.
  1. using System;  
  2. namespace QueueLib  
  3. {  
  4.     public interface IQueueService  
  5.     {  
  6.         void Send(string payload);  
  7.         string ConnectionString { getset; }  
  8.         string QueueName { getset; }  
  9.     }  
  10. }  
Next we will implement the AZQueue class that implements the IQueueService interface. This class will require the use of the Microsoft.Azure.Storage.Queue NuGet package. The class exposes two properties (ConnectionString and QueueName), along with the Send method.
  1. using System;  
  2. using Microsoft.Azure.Storage;   
  3. using Microsoft.Azure.Storage.Queue;   
  4.   
  5. namespace QueueLib  
  6. {  
  7.     public class AZQueue : IQueueService  
  8.     {  
  9.         public string ConnectionString { getset; }  
  10.         public string QueueName { getset; }  
  11.           
  12.         public void Send(string payload)  
  13.         {  
  14.             CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);  
  15.             CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();  
  16.             CloudQueue queue = queueClient.GetQueueReference(QueueName);  
  17.             CloudQueueMessage message = new CloudQueueMessage(payload);  
  18.             queue.AddMessage(message);  
  19.               
  20.         }  
  21.     }  
  22. }  
Finally, let’s implement the AZBus class that also implements the IQueueService interface. This class will require the use of Microsoft.Azure.ServiceBus NuGet package.
  1. using Microsoft.Azure.ServiceBus;  
  2. using System.Text;  
  3.   
  4. namespace QueueLib  
  5. {  
  6.     public class AZBus : IQueueService  
  7.     {  
  8.         public string ConnectionString { getset; }  
  9.         public string QueueName { getset; }  
  10.         private static IQueueClient queueClient;  
  11.         public void Send(string payload)  
  12.         {  
  13.             queueClient = new QueueClient(ConnectionString, QueueName);  
  14.             var message = new Message(Encoding.UTF8.GetBytes(payload));  
  15.             queueClient.SendAsync(message).Wait();  
  16.         }  
  17.     }  
  18. }  
Now that both classes and the interface have been created, let’s see the client application code.
 

Building the Client Console Application

 
The client application will be using the IQueueService interface to send its messages; a method called GetQueue() will have the logic necessary to figure out which queue to return (either AZBus or AZQueue). The key here is that the logic to select the correct queue object is abstracted from the main code; in this case, the logic simply looks at the ConnectionString value in the configuration file to make that determination. Since every service will have a different format for its connection string it can be as simple as inspecting its content to return the correct object. There are more robust ways to implement this logic, but for a simple case such as this one, it’s all we need. The appSettings of the client code looks like this,
  1. <appSettings>  
  2.   <add key="connectionString" value="yourconnectionstring"/>  
  3.   <add key="queueName" value="yourqueuename"/>  
  4. </appSettings>  
Once the object has been created, it’s simply a matter of calling the Send() method.
  1. using System;  
  2. using QueueLib;  
  3.   
  4. namespace ConsoleAppAZQueueDemo  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             var service = GetQueue();  
  11.             service.Send("test message");  
  12.   
  13.             Console.Read();  
  14.   
  15.         }  
  16.   
  17.         static IQueueService GetQueue()  
  18.         {  
  19.             // Central logic that returns the queue to use  
  20.             string connectionString = System.Configuration.ConfigurationManager.AppSettings["connectionString"];  
  21.             string queueName = System.Configuration.ConfigurationManager.AppSettings["queueName"];  
  22.   
  23.             // Detect which kind of queue we need to create, based on the connection string  
  24.             IQueueService service = null;  
  25.             if (connectionString.ToLower().Contains("core.windows.net"))  
  26.                 service = new AZQueue();  
  27.             else if (connectionString.ToLower().Contains("sb://"))  
  28.                 service = new AZBus();  
  29.   
  30.             service.ConnectionString = connectionString;  
  31.             service.QueueName = queueName;  
  32.   
  33.             return service;  
  34.         }  
  35.     }  
  36. }  

Testing with the Azure Queue

 
If the connection string contains core.windows.net, we know it’s an Azure Storage Queue; in the screenshot below the message sent can be seen in the Azure Portal directly.
 
Abstracting Azure Service Bus And Azure Queues To Send Messages
 

Testing with the Service Bus Queue

 
If the connection string contains sb://, we know it’s an Azure Service Bus; the screenshot below shows the message sent to the Azure Bus Queue.
 
Abstracting Azure Service Bus And Azure Queues To Send Messages
 

Conclusion

 
In this simple application we can see the benefits of abstracting the queue service in a library that will do the hard work for us. Changing the queue becomes as simple as changing the configuration settings of the application.
 
There are many opportunities to improve this simple code, such as the ability to receive messages, implementing queue-specific options, and a more robust strategy for choosing the queueing technology to implement just to name a few.