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.

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 implementing 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.
using System;
namespace QueueLib
{
public interface IQueueService
{
void Send(string payload);
string ConnectionString { get; set; }
string QueueName { get; set; }
}
}
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.
using System;
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Queue;
namespace QueueLib
{
public class AZQueue : IQueueService
{
public string ConnectionString { get; set; }
public string QueueName { get; set; }
public void Send(string payload)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
CloudQueue queue = queueClient.GetQueueReference(QueueName);
CloudQueueMessage message = new CloudQueueMessage(payload);
queue.AddMessage(message);
}
}
}



Guest UserPosted Aug 21, 2019, 3:23 AM
Hi Herver i need some clarity on two things please, one on Appsettings config file. first value is my connection string. To me on my understanding this you get on shared policies(Endpoint=sb://mobilesmstwo.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=*****=) second value is.....??? Last one is when instantiating new QueueClient(); get error stating no object found or missing some assemblies. What am i missing i need help please thanks.
Guest UserPosted Aug 16, 2019, 4:55 AM
Hi Herve, quick question the above samples seem be comprehensive. I want to find out, at what stage one is opted to use Azure Service or queue? How similar it is with sessions in azure?
Sean FeldmanPosted Aug 13, 2019, 9:44 AM
Abstracting infrastructure is a task that should not be taken lightly. Would recommend to look at already existing and mature libraries such as NServiceBus and MassTransit.