title | description | services | documentationcenter | author | manager | editor | ms.assetid | ms.service | ms.devlang | ms.topic | ms.tgt_pltfrm | ms.workload | ms.date | ms.author |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Get started with Azure Service Bus topics and subscriptions | Microsoft Docs |
Write a C# console application that uses Service Bus messaging topics and subscriptions. |
service-bus-messaging |
.net |
sethmanheim |
timlt |
service-bus-messaging |
tbd |
hero-article |
dotnet |
na |
10/10/2017 |
sethm |
[!INCLUDE service-bus-selector-topics]
This tutorial covers the following steps:
- Create a Service Bus namespace, using the Azure portal.
- Create a Service Bus topic, using the Azure portal.
- Create a Service Bus subscription to that topic, using the Azure portal.
- Write a console application to send a message to the topic.
- Write a console application to receive that message from the subscription.
- Visual Studio 2015 or higher. The examples in this tutorial use Visual Studio 2017.
- An Azure subscription.
[!INCLUDE create-account-note]
If you have already created a Service Bus Messaging namespace, jump to the Create a topic using the Azure portal section.
[!INCLUDE service-bus-create-namespace-portal]
-
Log on to the Azure portal.
-
In the left navigation pane of the portal, click Service Bus (if you don't see Service Bus, click More services, or click on All Resources).
-
Click the namespace in which you would like to create the topic. The namespace overview blade appears:
-
In the Service Bus namespace blade, click Topics, then click Add topic.
-
Enter a name for the topic, and uncheck the Enable partitioning option. Leave the other options with their default values.
-
At the bottom of the blade, click Create.
-
In the portal resources pane, click the namespace you created in step 1, then click name of the topic you created in step 2.
-
At the top of the overview pane, click the plus sign next to Subscription to add a subscription to this topic.
-
Enter a name for the subscription. Leave the other options with their default values.
To send messages to the topic, we write a C# console application using Visual Studio.
Launch Visual Studio and create a new Console app (.NET Framework) project.
-
Right-click the newly created project and select Manage NuGet Packages.
-
Click the Browse tab, search for WindowsAzure.ServiceBus, and then select the WindowsAzure.ServiceBus item. Click Install to complete the installation, then close this dialog box.
-
Add the following
using
statement to the top of the Program.cs file.using Microsoft.ServiceBus.Messaging;
-
Add the following code to the
Main
method. Set theconnectionString
variable to the connection string that you obtained when creating the namespace, and settopicName
to the name that you used when creating the topic.var connectionString = "<your connection string>"; var topicName = "<your topic name>"; var client = TopicClient.CreateFromConnectionString(connectionString, topicName); var message = new BrokeredMessage("This is a test message!"); client.Send(message); Console.WriteLine(String.Format("Message body: {0}", message.GetBody<String>())); Console.WriteLine(String.Format("Message id: {0}", message.MessageId)); Console.WriteLine("Message successfully sent! Press ENTER to exit program"); Console.ReadLine();
Here is what your Program.cs file should look like.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.ServiceBus.Messaging; namespace tsend { class Program { static void Main(string[] args) { var connectionString = "Endpoint=sb://<your namespace>.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=<your key>"; var topicName = "<your topic name>"; var client = TopicClient.CreateFromConnectionString(connectionString, topicName); var message = new BrokeredMessage("This is a test message!"); client.Send(message); Console.WriteLine(String.Format("Message body: {0}", message.GetBody<String>())); Console.WriteLine(String.Format("Message id: {0}", message.MessageId)); Console.WriteLine("Message successfully sent! Press ENTER to exit program"); Console.ReadLine(); } } }
-
Run the program, and check the Azure portal: click the name of your topic in the namespace Overview blade. The topic Essentials blade is displayed. In the subscription(s) listed near the bottom of the blade, notice that the Message Count value for each subscription should now be 1. Each time you run the sender application without retrieving the messages (as described in the next section), this value increases by 1. Also note that the current size of the topic increments the Current value on the Essentials blade each time the app adds a message to the topic/subscription.
-
To receive the message or messages you just sent, create a new console application and add a reference to the Service Bus NuGet package, similar to the previous sender application.
-
Add the following
using
statement to the top of the Program.cs file.using Microsoft.ServiceBus.Messaging;
-
Add the following code to the
Main
method. Set theconnectionString
variable to the connection string you obtained when creating the namespace, and settopicName
to the name that you used when creating the topic. Also make sure to replace<your subscription name>
with the name of the subscription you created in step 3.var connectionString = "<your connection string>"; var topicName = "<your topic name>"; var client = SubscriptionClient.CreateFromConnectionString(connectionString, topicName, "<your subscription name>"); client.OnMessage(message => { Console.WriteLine(String.Format("Message body: {0}", message.GetBody<String>())); Console.WriteLine(String.Format("Message id: {0}", message.MessageId)); }); Console.WriteLine("Press ENTER to exit program"); Console.ReadLine();
Here is what your Program.cs file should look like:
using System; using Microsoft.ServiceBus.Messaging; namespace GettingStartedWithTopics { class Program { static void Main(string[] args) { var connectionString = "Endpoint=sb://<your namespace>.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=<your key>";; var topicName = "<your topic name>"; var client = SubscriptionClient.CreateFromConnectionString(connectionString, topicName, "<your subscription name>"); client.OnMessage(message => { Console.WriteLine(String.Format("Message body: {0}", message.GetBody<String>())); Console.WriteLine(String.Format("Message id: {0}", message.MessageId)); }); Console.WriteLine("Press ENTER to exit program"); Console.ReadLine(); } } }
-
Run the program, and check the portal again. Notice that the Message Count and Current values are now 0.
Congratulations! You have now created a topic and subscription, sent a message, and received that message.
Check out our GitHub repository with samples that demonstrate some of the more advanced features of Service Bus messaging.