Get started with Service Bus queues

What will be accomplished

This tutorial covers the following steps:

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

Prerequisites

  1. Visual Studio 2015 or higher. The examples in this tutorial use Visual Studio 2017.
  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've already created a Service Bus Messaging namespace, jump to the Create a queue using the Azure portal section.

To begin using Service Bus messaging entities in Azure, you must first create a namespace with a name that is unique across Azure. 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

Creating a new namespace will automatically generate an initial Shared Access Signature (SAS) rule with an associated pair of primary and secondary keys that grants full control over all aspects of the namespace. Refer to Service Bus authentication and authorization for how to create further rules with more constrained rights for regular senders and receivers. To copy the initial rule, follow these steps:

  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 have already created a Service Bus queue, 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. Click the namespace in which you would like to create the queue. 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 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 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 to the connection string that you obtained when creating the namespace, and set queueName to the queue name that you 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!");
    
    Console.WriteLine(String.Format("Message id: {0}", message.MessageId));
    
    client.Send(message);
    
    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 qsend
    {
        class Program
        {
            static void Main(string[] args)
            {
                var connectionString = "Endpoint=sb://<your namespace>.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=<your key>";
                var queueName = "<your queue name>";
    
                var client = QueueClient.CreateFromConnectionString(connectionString, queueName);
                var message = new BrokeredMessage("This is a test message!");
    
                Console.WriteLine(String.Format("Message id: {0}", message.MessageId));
    
                client.Send(message);
    
                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 queue in the namespace Overview window. The queue Essentials window is displayed. Notice that the Active Message Count value should now be 1. Each time you run the sender application without retrieving the messages, this value increases by 1. Also note that the current size of the queue increments each time the app adds a message to the queue.

    Message size

4. Receive messages from the queue

  1. To receive the 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 that was obtained when creating the namespace, and set queueName to the queue name that you used when creating the queue.

    var connectionString = "<your connection string>";
    var queueName = "<your queue name>";
    
    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.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 GettingStartedWithQueues
    {
      class Program
      {
        static void Main(string[] args)
        {
          var connectionString = "Endpoint=sb://<your namespace>.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=<your key>";;
          var queueName = "<your queue name>";
    
          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.WriteLine("Press ENTER to exit program");   
          Console.ReadLine();
        }
      }
    }
    
  4. Run the program, and check the portal again. Notice that the Active Message Count and Current values are now 0.

    Queue length

Congratulations! You have now created a queue, sent a message to that queue, and received a message from the same queue.

Next steps

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