In this article, we will discuss an Azure service bus topic example using c#. We will discuss how to send messages using the Service bus topic and subscription via a console application.
Let’s discuss the prerequisites needed to perform this example. Below are the lists of prerequisites needed here.
- You must have an Azure Subscription. There is nothing to worry about if you don’t have an Azure account. Create an Azure free account now.
- You must install Visual Studio 2019 or a higher version on your PC.
On a high level, below is the activity we need to perform here.
- We need to create an Azure Service bus namespace in Azure Portal.
- Creating Azure service bus topics in Azure Portal.
- Creating a subscription for the topic in Azure Portal.
- Create a console application using Visual Studio 2019 to send and receive messages.
Since I have already created the Azure service bus namespace, I will use that existing namespace here, and you can refer to the above link to create the Azure service bus namespace.
Let’s create the Azure service bus topics in Azure Portal.
How to create Azure service bus topics in Azure Portal
Follow the below steps to create the Azure service bus topic.
- Log in to the Azure Portal.
- Search for service bus and click on the search result service bus.

3. Now, you will see the Azure Service Bus namespaces lists. Click on the one where you want to create the topic.

4. On the Service Bus Namespace page, click the + Topic button to create the Azure Service Bus Topic.

Or, for the same option, you can click on the Topics link under Entities from the left navigation –> Then click on the + Topic button.

5. On the Create topic window, provide the below details
- Name: Provide a unique name for the topic.
- Max topic size: Provide the maximum topic size as per your requirement.
- Message time to live: Mention the time for the message to live.
- Enable auto-delete on idle topic: Select this option if you want to enable the automatic deletion of the idle topic.
- Enable duplicate detection: Select this option if you wish to enable the option to detect duplicates automatically.
- Enable Partitioning: Check this option if you wish to enable the Partitioning option.
After providing all the above details, click the Create button to create the topic in Azure Service Bus.

It took seconds to create the Azure Service Bus topic successfully.

To view the topic we have created, on the Service Bus Namespace page, click on the Topics link that is present under Entities from the left navigation. On the right side, you can see the topic that we have created.

So, our topic is ready. Now, let’s create a subscription for the topic we have created.
Creating Azure Service Bus topic subscription
- On the Service Bus Topic page, click on the + Subscription button.

2. On the Create subscription window, provide the below details
- Name: Provide a unique name for the subscription.
- Max delivery count: Specify the maximum delivery count.
- Auto-delete after idle for: Mention days and time-based on your requirement.
- Never auto-delete: Check this option if you don’t want the auto-delete option.
- Forward messages to queue/topic: Select this option if you wish to forward messages to the topic or queue.
- Enable sessions: You can enable this option to help you first-in-first-out delivery of messages.
- Message time to live: Specify the duration for the messages to live.
- Enable dead lettering on message expiration: Select this option if you wish to enable the dead lettering on the message expiration.
- Move messages that cause filter evaluation exceptions to the dead-letter sub- queue: You can enable this option if you want to move the messages to the dead-letter sub-queue responsible for the exceptions.
- Message Lock Duration: If you wish to apply the lock for the messages, you need to specify the duration for the lock operation.


After providing all the details, click the Create button to create the subscription for the topic.
Now, we have created the Azure Service bus subscription successfully.

Moving to the next steps, as part of this demo, we need to create a console application using Visual Studio 2019 that will help us se