Post

Publish Messages to Azure ServiceBus Queue

Publsih messages to Azure ServiceBus Queue using dotnet core and Azure SDK

In this post, I will show you how to publish messages to an Azure ServiceBus Queue using dotnet core and Azure SDK.

Prerequisites

  • Azure Subscription
  • Azure ServiceBus Namespace
  • Azure ServiceBus Queue
  • Azure ServiceBus Connection String

Create Azure ServiceBus Namespace and Queue

Create an Azure ServiceBus Namespace and Queue using the following steps:

  1. Login to the Azure Portal.
  2. Click on the Create a resource button.
  3. Search for Service Bus and click on the Create button.
  4. Enter the required details like Subscription, Resource Group, Namespace Name, Location, etc.
  5. Click on the Review + Create button.
  6. Click on the Create button to create the Azure ServiceBus Namespace.

Once the Azure ServiceBus Namespace is created, create a new Queue using the following steps:

  1. Navigate to the Azure ServiceBus Namespace.
  2. Click on the Queues under the Entities section.
  3. Click on the + Queue button.
  4. Enter the required details like Queue Name, Queue Size, Message Time To Live, etc.
  5. Click on the Create button to create the Azure ServiceBus Queue.

Create a new dotnet core console application

Create a new dotnet core console application using the following command:

1
2
dotnet new console -n PublishMessagesToServiceBusQueue
cd PublishMessagesToServiceBusQueue

Install Azure.Messaging.ServiceBus package

Install the Azure.Messaging.ServiceBus package using the following command:

1
dotnet add package Azure.Messaging.ServiceBus

Publish messages to Azure ServiceBus Queue

Add the following code in Program.cs file to publish messages to the Azure ServiceBus Queue:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus;

namespace PublishMessagesToServiceBusQueue
{
    class Program
    {
        static async Task Main(string[] args)
        {
            string connectionString = "<connection-string>";
            string queueName = "<queue-name>";

            await SendMessageToQueue(connectionString, queueName);
        }

        static async Task SendMessageToQueue(string connectionString, string queueName)
        {
            await using var client = new ServiceBusClient(connectionString);
            ServiceBusSender sender = client.CreateSender(queueName);

            var message = new ServiceBusMessage("Hello, World!");

            await sender.SendMessageAsync(message);
            Console.WriteLine($"Sent a single message to the queue: {queueName}");
        }
    }
}

Run the application

Run the application using the following command:

1
dotnet run

The application will publish a message to the Azure ServiceBus Queue.

That’s it! You have successfully published messages to an Azure ServiceBus Queue using dotnet core and Azure SDK.

You can view your messages from the Azure Portal by navigating to the Azure ServiceBus –> Queues and select the respective queue you are using for publish.

This post is licensed under CC BY 4.0 by the author.