Take advantage of Azure Service Bus queues for scalable, cloud-based, asynchronous, and reliable messaging in your .Net applications Credit: Thinkstock A queue is a data structure that works on a FIFO (first in first out) basis. Items are inserted at the end of the queue and removed from the beginning. The term “Enqueue” describes the operation that inserts data in the queue, while the term “Dequeue” denotes the removal of data from the queue. Azure Service Bus is a scalable message fabric that provides reliable messaging as an Azure cloud service. You can use it for three different types of messaging: service relays between on-prem and cloud environments, topics for one-to-many publish/subscribe communications, and queues. This article will illustrate how we can work with Azure Service Bus queues using .Net. Getting started with Azure Service Bus First you will need to create a free Microsoft Azure account if you don’t have one. Then, to work with Azure Service Bus queues, we will follow these steps. Create a Service Bus namespace (if you don’t already have one) in the Azure portal Create a Service Bus queue in the Azure portal Create an application to send and receive one or more messages to the queue Creating a Service Bus namespace in Azure Assuming that you have created an account in Azure, follow these steps to create a Service Bus in Azure. Log into the Azure portal using your credentials Click Create a resource -> Enterprise Integration -> Service Bus Provide the name of the namespace, choose the appropriate pricing tier, and choose or provide a resource group Click Create when done You can optionally click the “Pin to dashboard” checkbox to ensure that the resource you create can be viewed in the dashboard of your Azure account as well. To view the primary and secondary keys and connection strings, select the namespace you just created and then click on Shared Access Policies -> RootManageSharedAccessKey. Creating and configuring a Service Bus queue in Azure Now that the Service Bus has been created, follow these steps to create a new queue. From the Azure Dashboard, select the Service Bus you just created to open the Service Bus Namespace window From the menu at the top of the Service Bus Namespace window, click Queue to open the Create queue window In the Create queue window, enter the name of the queue and specify the values for the properties (max size, message time to live, duration, etc.) as appropriate Click Create when done And that’s it! You have successfully created a Service Bus namespace and a Service Bus queue in Azure. You can refer to the notifications displayed in your Azure portal for the status of these operations. Once the queue has been successfully created, the message “Your queue was successfully created” will appear at the top right corner of the screen. Programming the Azure Service Bus queue in .Net Let’s now dig into the coding. In this section we will explore how we can send and retrieve messages to and from the Azure Service Bus queue. Create a console application project in Visual Studio and install the WindowsAzure.ServiceBus package via the NuGet Package Manager. The following code snippet illustrates how you can send a message to a queue. Note how the SendAsync method has been used to send a message asynchronously. public async static Task SendMessageAsync() { var connectionString ="Specify the connection string here."; var queue ="Specify the name of the queue here."; var queueClient = QueueClient.CreateFromConnectionString(connectionString, queue); var brokeredMessage = new BrokeredMessage(“Test Message”); await queueClient.SendAsync(brokeredMessage); await queueClient.CloseAsync(); } To retrieve messages from the queue, you can use the ReceiveAsync method as shown below. public async static Task GetMessageAsync() { var connectionString ="Specify the connection string here."; var queue ="Specify the name of the queue here."; var queueClient = QueueClient.CreateFromConnectionString(connectionString, queue); var brokeredMessage = await queueClient.ReceiveAsync(); string text = brokeredMessage.GetBody<string>(); Console.WriteLine(text); await queueClient.CloseAsync(); } Note that for the sake of simplicity our GetMessageAsync method above uses Console.WriteLine to display a message retrieved from the queue at the console window. Ideally, you should return the message back to the caller method, i.e., the method from which the GetMessageAsync method would be called. The snippet below shows how you can invoke these asynchronous methods from the Main method. static void Main(string[] args) { SendMessageAsync().GetAwaiter().GetResult(); GetMessageAsync().GetAwaiter().GetResult(); Console.Read(); } Finally, you should ensure that the following namespaces are included in your program. using System.Threading; using System.Threading.Tasks; using Microsoft.Azure.ServiceBus; Brokered messaging such as that provided by Azure Service Bus enables you to build a scalable, distributed, loosely coupled, event-driven, service-oriented architecture. In this post we have seen how we can work with an Azure Service Bus queue in .Net. I plan to discuss more advanced concepts related to Azure Service Bus in a future post here. Related content news Red Hat OpenShift AI unveils model registry, data drift detection Cloud-based AI and machine learning platform also adds support for Nvidia NIM, AMD GPUs, the vLLM runtime for KServe, KServe Modelcars, and LoRA fine-tuning. By Paul Krill Nov 12, 2024 3 mins Generative AI PaaS Artificial Intelligence feature What is cloud computing? Everything you need to know Cloud computing has become the ideal way to deliver enterprise applications—and the preferred solution for companies extending their infrastructure or launching new innovations. By Eric Knorr and dan_muse Nov 01, 2024 18 mins Hybrid Cloud IaaS PaaS feature Rapid B2B integrations with Ballerina and Choreo How WSO2’s Ballerina language and Choreo platform can be used to quickly develop, test, and deploy partner-specific EDI processing modules. By Chathura Ekanayake Apr 08, 2024 9 mins Development Libraries and Frameworks PaaS Programming Languages feature 7 mistakes to avoid when developing RPAs Bots at their best offer a high return on investment—but there are risks. Here are seven mistakes software developers should watch out for. By Isaac Sacolick Oct 23, 2023 8 mins PaaS Robotics SaaS Resources Videos