Introduction

In my last article, I discussed about Azure Service Bus Queue, which is basically a one to one communication. This article tells you how to work with Azure Service Bus topics with a sample console application.

Content

  • Service Bus Topics
  • Create Service Bus Topics and subscriptions on the Azure portal
  • Send and receive a message using service bus topics

Pre-request

  • Visual Studio 2017 update 3 or later
  • Azure Subscription
  • Basic Knowledge of the Azure portal
Azure Service Bus Topics

Topics is similar to Queue but Inside a topic, we have subscriptions and each subscription will have multiple subscribers. The process is simple - just put data to the subscription and one or more subscriber will pull the data out.

Creating a Service Bus Topic in Azure Portal

Step 1

Login into Azure Portal https://portal.azure.com/

Step 2

I’m going to use an existing Azure Service Bus namespace which is used for creating a Queue, please refer to the last article where I have explained how to create an Azure Service Bus and it’ namespace.

Step 3

Once the Azure Service Bus has been created successfully, now, we can start to create a topic for the service bus. Working with topics is similar to queue in Azure Service Bus.

Step 4

Go to Azure service bus namespace. In my case I’m using my existing namespace msgdemobus .

Step 5

In overview, you can find a topic, click on it to add a new topic as shown in the below figure.

Step 6 - Add Topic

  • Give the name for the topic, in my case I named it as mytopics
  • Set a max topic size based on the requirement
  • Time for message to live -- by default it will be 14 days
  • By enabling duplicate detection, topic will not store any duplicate messages
  • By enabling the partioning, the topics itself spreads into multiple storage systems, that means more than one message broker will be backing up the message.

Finally click on create.

Step 7

Once the topic is created successfully, we are able to create a subscription for the topic. In topic overview, click on subscription as shown below.

Step 8

Add the subscription, name the subscription, and I'm going with default values for other fields, as shown below,

Send and receive a message using Service Bus Topics

I’m going to create a new console application using C# to send and receive a message with Azure Service Bus Topics using Topic Client.

MyTopicApp.cs

  1. using Microsoft.ServiceBus.Messaging;
  2. using System;
  3. namespace MyTopicApp
  4. {
  5. class Program
  6. {
  7. private static string _serviceBusConn = "Endpoint=sb://<namespace>.servicebus.windows.net/;SharedAccessKeyName=<policy name>;SharedAccessKey=<key>";
  8. private static string _serviceBustopic = "<Topic Name>";
  9. static void Main(string[] args)
  10. {
  11. SendMessage("Hello I'm Azure Service Bus Topic ");
  12. }
  13. static void SendMessage(string message)
  14. {
  15. var topicClient = TopicClient.CreateFromConnectionString(_serviceBusConn, _serviceBustopic);
  16. var msg = new BrokeredMessage(message);
  17. topicClient.Send(msg);
  18. }
  19. static void ReadMessage()
  20. {
  21. var subClient = SubscriptionClient.CreateFromConnectionString(_serviceBusConn, _serviceBustopic, "<subscription name>");
  22. subClient.OnMessage(m =>
  23. {
  24. Console.WriteLine(m.GetBody<string>());
  25. });
  26. }
  27. }
  28. }

_serviceBustopic will holds a topic Name.

_serviceBusConn will hold the Azure service bus connection string, we can get a shared access Name and key from Azure service bus shared access policy as showed in the below figure.
Policy Name
Pick a key.

Send Message

SendMessage function is used to send a message to Azure service bus topic using topic client and the BrokeredMessage is used to hold the message.

  1. static void Main(string[] args)
  2. {
  3. SendMessage("Hello I'm Azure Service Bus Topic ");
  4. }
  5. static void SendMessage(string message)
  6. {
  7. var topicClient = TopicClient.CreateFromConnectionString(_conn, _topic);
  8. var msg = new BrokeredMessage(message);
  9. topicClient.Send(msg);
  10. }
Run the program

Switch to Azure portal, you can notice the message count will be updated from 0 to 1.

Read Message

ReadMessage function is used to received a message based on the subscription using subscription client.

  1. static void Main(string[] args)
  2. {
  3. ReadMessage();
  4. Console.ReadKey();
  5. }
  6. static void ReadMessage()
  7. {
  8. var subClient = SubscriptionClient.CreateFromConnectionString(_serviceBusConn, _serviceBustopic, "MyAppSubscription");
  9. subClient.OnMessage(m =>
  10. {
  11. Console.WriteLine(m.GetBody<string>());
  12. });
  13. }
Run a program

Result
Message is received from topic through subscription.
Switch to the Azure portal and you can notice the message count will be updated from 1 to 0.
I hope you have enjoyed this article. Your valuable feedback, questions, or comments about this article are always welcome.