Get started with Service Bus queues

What will be accomplished

In this tutorial, we will complete the following:

  1. Create a Service Bus namespace, using the Azure portal.
  2. Create a Service Bus Messaging queue, using the Azure portal.
  3. Write a console application to send a message.
  4. Write a console application to receive messages.

Prerequisites

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

To complete this tutorial, you need an Azure account. You can activate your MSDN subscriber benefits or sign up for a free account.

1. Create a namespace using the Azure portal

If you already have a Service Bus namespace created, jump to the Create a queue using the Azure portal section.

Create a service namespace

To begin using Service Bus queues in Azure, you must first create a namespace. A namespace provides a scoping container for addressing Service Bus resources within your application.

To create a namespace:

  1. Log on to the Azure portal.
  2. In the left navigation pane of the portal, click New, then click Enterprise Integration, and then click Service Bus.
  3. In the Create namespace dialog, enter a namespace name. The system immediately checks to see if the name is available.
  4. After making sure the namespace name is available, choose the pricing tier (Basic, Standard, or Premium).
  5. In the Subscription field, choose an Azure subscription in which to create the namespace.
  6. In the Resource group field, choose an existing resource group in which the namespace will live, or create a new one.
  7. In Location, choose the country or region in which your namespace should be hosted.

    Create namespace

  8. Click Create. The system now creates your namespace and enables it. You might have to wait several minutes as the system provisions resources for your account.

Obtain the management credentials

  1. In the list of namespaces, click the newly created namespace name.
  2. In the namespace blade, click Shared access policies.
  3. In the Shared access policies blade, click RootManageSharedAccessKey.

    connection-info

  4. In the Policy: RootManageSharedAccessKey blade, click the copy button next to Connection string–primary key, to copy the connection string to your clipboard for later use. Paste this value into Notepad or some other temporary location.

    connection-string

  5. Repeat the previous step, copying and pasting the value of Primary key to a temporary location for later use.

2. Create a queue using the Azure portal

If you already have a Service Bus queue created, jump to the Send messages to the queue section.

Please ensure that you have already created a Service Bus namespace, as shown here.

  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).
  3. Select the namespace that you would like to create the queue in. In this case, it is nstest1.

    Create a queue

  4. In the Service Bus namespace blade, select Queues, then click Add queue.

    Select Queues

  5. Enter the Queue Name and leave the other values with their defaults.

    Select New

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

3. Send messages to the queue

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

Create a console application

  • Launch Visual Studio and create a new Console application.

Add the Service Bus NuGet package

  1. Right-click the newly created project and select Manage NuGet Packages.
  2. Click the Browse tab, then search for “Microsoft Azure Service Bus” and select the Microsoft Azure Service Bus 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 queue

  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 as the connection string that was obtained when creating the namespace, and set queueName as the queue name that used when creating the queue.

    var connectionString = "<Your connection string>";
    var queueName = "<Your queue name>";
    
    var client = QueueClient.CreateFromConnectionString(connectionString, queueName);
    var message = new BrokeredMessage("This is a test message!");
    client.Send(message);
    

    Here is what your Program.cs should look like.

    using System;
    using Microsoft.ServiceBus.Messaging;
    
    namespace GettingStartedWithQueues
    {
        class Program
        {
            static void Main(string[] args)
            {
                var connectionString = "<Your connection string>";
                var queueName = "<Your queue name>";
    
                var client = QueueClient.CreateFromConnectionString(connectionString, queueName);
                var message = new BrokeredMessage("This is a test message!");
    
                client.Send(message);
            }
        }
    }
    
  3. Run the program, and check the Azure portal. Click the name of your queue in the namespace Overview blade. Notice that the Active message count value should now be 1.

    Message count

4. Receive messages from the queue

  1. Create a new console application and add a reference to the Service Bus NuGet package, similar to the previous sending 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 as the connection string that was obtained when creating the namespace, and set queueName as the queue name that you used when creating the queue.

    var connectionString = "";
    var queueName = "samplequeue";
    
    var client = QueueClient.CreateFromConnectionString(connectionString, queueName);
    
    client.OnMessage(message =>
    {
      Console.WriteLine(String.Format("Message body: {0}", message.GetBody<String>()));
      Console.WriteLine(String.Format("Message id: {0}", message.MessageId));
    });
    
    Console.ReadLine();
    

    Here is what your Program.cs file should look like:

    using System;
    using Microsoft.ServiceBus.Messaging;
    
    namespace GettingStartedWithQueues
    {
      class Program
      {
        static void Main(string[] args)
        {
          var connectionString = "";
          var queueName = "samplequeue";
    
          var client = QueueClient.CreateFromConnectionString(connectionString, queueName);
    
          client.OnMessage(message =>
          {
            Console.WriteLine(String.Format("Message body: {0}", message.GetBody<String>()));
            Console.WriteLine(String.Format("Message id: {0}", message.MessageId));
          });
    
          Console.ReadLine();
        }
      }
    }
    
  4. Run the program, and check the portal. Notice that the Queue Length value should now be 0.

    Queue length

Congratulations! You have now created a queue, sent a message, and received a message.

Next steps

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