Azure Service Bus Queue Example C#

Azure Service Bus Queue Example C#

In this Azure article, we will discuss an example of an Azure Service bus queue using C#. Along with that, we will also discuss a few other topics.

Azure Service Bus Queue Example C#

Below are the prerequisites needed here to work with this Azure service bus queue implementation example. Below are the lists of prerequisites needed here.

Prerequisites

  • You must have an Azure Subscription. If you don’t have an Azure account, create an Azure free account now.
  • You will have to install Visual Studio 2019 on your development machine.

Overall, below are the steps we are going to perform here as part of this example.

  1.  Create an Azure Service bus namespace in Azure Portal.
  2. Creating an Azure Service bus queue in Azure Portal (Follow step-7 to step-9).
  3. Creating a console application using Visual Studio 2019 for sending and receiving messages.

I have already created an Azure service bus namespace, the name tsinfo, and also an Azure Service bus queue and the name tsinfoqueue by following the above articles. Assuming you are ready with all the prerequisites, let’s create a console application using Visual Studio 2019.

Creating a console application using Visual Studio 2019 for sending and receiving messages

Follow the below steps

  1. Open Visual Studio 2019 on your Dev machine.
  2. Click on Create a new project button.
  3. Select the C# Windows Console application template and click the Next button.
azure service bus implementation c#

4. On the Configure your new project window, provide a name for your project, Choose the location where you wish to save your console application, Provide the solution name, and ensure the Framework is .NET Framework 4.7.2 or higher. Click on the Create button to create the console application.

service bus azure c#

Use the below code in your Program.cs file and modify the namespace name, service bus queue name, connection string, etc.

You can find the connection string value from the Azure Portal by following how to find Azure service bus connection string.

using System;
using System.Linq;
using System.Activities;
using System.Activities.Statements;
using Microsoft.ServiceBus.Messaging;

namespace ServiceBusQueueExamples
{

    class Program
    {
        public static string conn = "Endpoint=sb://tsinfo.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=XESuR3Ce1x08G08pXs3C7SIQPAgj0/yIXfXr8AzVoos=";
        public static string servicebusqueuename = "tsinfoqueue";

        public static void Main(string[] args)
        {
          
           SendingMessageToQueue("Hello This is a Hii from Service Bus Queue ");
           GettingMessage();
            Console.ReadKey();
        }

        public static void SendingMessageToQueue(string message)
        {
            var tC = QueueClient.CreateFromConnectionString(conn , servicebusqueuename);
            var msg1 = new BrokeredMessage(message);
            tC.Send(msg1);
        }

        public static void GettingMessage()
        {
            var qC = QueueClient.CreateFromConnectionString(conn , servicebusqueuename);
            BrokeredMessage msg2 = qC.Receive();
            string body = msg2.GetBody<string>();
            msg2.Complete();
            msg2.Abandon();
            Console.WriteLine(body);
            Console.ReadLine();
        }

    }
}

Once you copy and paste this code, You will see the error for QueueClient and BrokeredMessage like below i.e., “the type or namespace name ‘brokeredmessage’ could not be found” and ‘the name QueueClient does not exist in the current context. to fix these errors, click on the dropdown and then select Install package ‘WindowsAzure.ServiceBus’ –> Click on the Find and install latest version option.

azure bus service c#
azure service bus c# example

Press F5 to run the console application, and once you run the application, you will get the below-expected output.

azure service bus example c#

For better clarity, what you can do is, in the Main (), comment the GettingMessage() and press F5 to run the application.

 public static void Main(string[] args)
        {
          
           SendingMessageToQueue("Hello This is a Hii from Service Bus Queue ");
           //GettingMessage();
            Console.ReadKey();
        }

Now, navigate to the Azure Service Bus Queue in Azure Portal. You can able to see the Active Message count is 1.

azure service bus queue example

Now, comment on the SendingMessageToQueue() method, uncomment the GettingMessage() method, and press F5 to run the console application.

 public static void Main(string[] args)
        {
          
           //SendingMessageToQueue("Hello This is a Hii from Service Bus Queue ");
           GettingMessage();
            Console.ReadKey();
        }

You can see the output below

azure service bus tutorial

Now, if you navigate to the Azure Portal and on Azure Service Queue, you will find the Active message count as 0, and you are done !!!

azure service bus c#

You may also like following the below Azure articles

Wrapping Up

This article discussed a real-time example of Azure Service Bus Queue using C#. Thanks for reading this article !!!