Azure Service Bus - Topic
This articles explains about Azure Service Bus Topic and Subscription which is commonly known as pub-sub with a real world scenario.
Business Requirement
The business requirement is in my previous article,
So far we have seen about mobile recharge where we have used queuing mechanism. Now a company wants to provide some offers on regular recharge. But how will registered users come to know about these offers?
So again the company has started looking for some solution and finally it has come up with the solution of Azure Service Bus - Topic. Topic works like pub-sub model where one is the publisher and there can be many subscribers for the same publisher. In our scenario the publisher will publish offers and subscribed users will get the offers immediately.
Now let's see the step by step implementation of the below to design a solution:
- Log in to the Azure portal via portal.azure.com
- Explore previously created Service Namespace; i.e. Mobile Recharge
- Click on '+Topic' under Topics to create a new topic

- Give appropriate name for Topic
- Keep the rest of the input values as is and click on 'Create' button

We can see that Topic is created in it is shown under topics list.
- Click on previously created topic; i.e. 'Offers', which will show the list of subscriptions. As of now it shows empty
- Click on '+Subscription' under subscriptions to create subscription

- Give proper name of subscription
- Change Lock duration to 5 minutes and click on 'Create' button, which will create subscription for topic

- Follow the preceding steps and create one more subscription

We can see both the subscriptions in the list.
Credentials
Click on 'Shared access policies' from the left panel, click on 'RootManageSharedAccessKey' to explore keys and connection strings which will be used further to connect.
Publish Message
- Create a console application in Visual Studio
- Set variables, one for connection string which you can copy from Shared access policies section and another is topic name; i.e. offers
- In order to publish the offers, we need an offer from user
- Add Microsoft.Azure.ServiceBus from a NuGet package manager
- Create topic client using connection string and queue name
- Convert string message to Azure Service Bus message
- Using topic client, call SendAsync method to publish the message.
- Call CloseAsync to close opened connection in finally block.
- class Program
- {
- static ITopicClient topicClient;
- static void Main(string[] args)
- {
- string sbConnectionString = "Endpoint=sb://mobilerecharge.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=KVb9ubc9XaV0dT/1dMjW9CzPWvA/JGvVvUZ64U21IBI=";
- string sbTopic = "offers";
- string messageBody=string.Empty;
- try
- {
- Console.WriteLine("-------------------------------------------------------");
- Console.WriteLine("Publish Offer");
- Console.WriteLine("-------------------------------------------------------");
- Console.WriteLine("Offers");
- Console.WriteLine("1. Recharge with 100 and get talk time of 110");
- Console.WriteLine("2. Get 5 GB data on recharge of 300. Validity 28 days");
- Console.WriteLine("3. 1000 SMS in recharge of 100");
- Console.WriteLine("-------------------------------------------------------");
- Console.WriteLine("Offer:");
- string offer = Console.ReadLine();
- Console.WriteLine("-------------------------------------------------------");
- switch (offer)
- {
- case "1":
- offer = "Recharge with 100 and get talk time of 110";
- break;
- case "2":
- offer = "Get 5 GB data on recharge of 300. Validity 28 days";
- break;
- case "3":
- offer = "1000 SMS in recharge of 100";
- break;
- default:
- break;
- }
- messageBody = offer;
- topicClient = new TopicClient(sbConnectionString, sbTopic);
- var message = new Message(Encoding.UTF8.GetBytes(messageBody));
- Console.WriteLine($"Message Published: {messageBody}");
- topicClient.SendAsync(message);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- finally
- {
- Console.ReadKey();
- topicClient.CloseAsync();
- }
- }
- }




Prasad KawadePosted Jun 30, 2021, 9:58 AM
Console.ReadLine is it mandatory part ? How can I parse message received ? I can't access any variable inside method ReceiveMessageAsync
Elva satriaPosted Aug 22, 2019, 5:41 AM
How to receive in asp web form to show in browser?
Elva satriaPosted Aug 7, 2019, 2:36 AM
How to add topic and subscription in class?
Sagar ShindePosted Mar 6, 2019, 1:50 AM
Good article... thanks
Rushi MehtaPosted Nov 1, 2018, 6:33 AM
Nicely Explained