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 then click on the Next button.
send message to azure service bus queue 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 make sure the Framework is .NET Framework 4.7.2 or higher. Click on the Create button to create the console application.

read message from azure service bus queue c sharp

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 will copy 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‘. in order to fix these errors, click on the dropdown and then select Install package ‘WindowsAzure.ServiceBus’ –> Click on the Find and install latest version option.

the name QueueClient does not exist in the current context
the type or namespace name ‘brokeredmessage’ could not be found error

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

Azure Service Bus Queue Example using 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 the SendingMessageToQueue() method and 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 able to see the below output

Azure Service Bus Queue Example using C sharp

Now, if you will 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 Queue Example C sharp

You may also like following the below Azure articles

Wrapping Up

In this article, we discuss a real time example of Azure Service Bus Queue using C#. Thanks for reading this article !!!