Skip to content

Files

Latest commit

author
Emanuele B
Oct 25, 2017
39984e3 · Oct 25, 2017

History

History
221 lines (159 loc) · 9.4 KB

service-bus-dotnet-how-to-use-topics-subscriptions.md

File metadata and controls

221 lines (159 loc) · 9.4 KB
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

Get started with Service Bus topics

[!INCLUDE service-bus-selector-topics]

This tutorial covers the following steps:

  1. Create a Service Bus namespace, using the Azure portal.
  2. Create a Service Bus topic, using the Azure portal.
  3. Create a Service Bus subscription to that topic, using the Azure portal.
  4. Write a console application to send a message to the topic.
  5. Write a console application to receive that message from the subscription.

Prerequisites

  1. Visual Studio 2015 or higher. The examples in this tutorial use Visual Studio 2017.
  2. An Azure subscription.

[!INCLUDE create-account-note]

1. Create a namespace using the Azure portal

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]

2. Create a topic using the Azure portal

  1. Log on to the Azure portal.

  2. 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).

  3. Click the namespace in which you would like to create the topic. The namespace overview blade appears:

    Create a topic

  4. In the Service Bus namespace blade, click Topics, then click Add topic.

    Select Topics

  5. Enter a name for the topic, and uncheck the Enable partitioning option. Leave the other options with their default values.

    Select New

  6. At the bottom of the blade, click Create.

3. Create a subscription to the topic

  1. In the portal resources pane, click the namespace you created in step 1, then click name of the topic you created in step 2.

  2. At the top of the overview pane, click the plus sign next to Subscription to add a subscription to this topic.

    Create subscription

  3. Enter a name for the subscription. Leave the other options with their default values.

4. Send messages to the topic

To send messages to the topic, we write a C# console application using Visual Studio.

Create a console application

Launch Visual Studio and create a new Console app (.NET Framework) project.

Add the Service Bus NuGet package

  1. Right-click the newly created project and select Manage NuGet Packages.

  2. 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.

    Select a NuGet package

Write some code to send a message to the topic

  1. Add the following using statement to the top of the Program.cs file.

    using Microsoft.ServiceBus.Messaging;
  2. Add the following code to the Main method. Set the connectionString variable to the connection string that you obtained when creating the namespace, and set topicName 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();
            }
        }
    }
  3. 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.

    Message size

5. Receive messages from the subscription

  1. 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.

  2. Add the following using statement to the top of the Program.cs file.

    using Microsoft.ServiceBus.Messaging;
  3. Add the following code to the Main method. Set the connectionString variable to the connection string you obtained when creating the namespace, and set topicName 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();
        }
      }
    }
  4. Run the program, and check the portal again. Notice that the Message Count and Current values are now 0.

    Topic length

Congratulations! You have now created a topic and subscription, sent a message, and received that message.

Next steps

Check out our GitHub repository with samples that demonstrate some of the more advanced features of Service Bus messaging.