Azure Service Bus Queues

Service Bus queues

Use the following steps to create a Service Bus Queue.

  1. Create a Service Bus namespace, using the Azure portal.
  2. Create a Service Bus queue, using the Azure portal.
  3. Write a .NET console application to send a set of messages to the queue.
  4. Write a .NET console application to receive those messages from the queue.

Prerequisites

  1. Visual Studio 2017 or later.
  2. An Azure subscription.

To create a namespace

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

    Azure

  7. 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. Click All resources, then click the newly created namespace name.
  2. In the namespace window, click Shared access policies.
  3. In the Shared access policies screen, click RootManageSharedAccessKey.

    Azure

  4. In the Policy: RootManageSharedAccessKey window, 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.

    Azure

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

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 All services).
  3. Click the namespace in which you would like to create the queue. In this case, it is sbnstest1.
  4. In the namespace window, click Queues, then in the Queues window, click + Queue.
  5. Enter the queue Name and leave the other values with their defaults.
  6. At the bottom of the window, click Create.

Send messages to the queue

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

Launch Visual Studio and create a new Console App (.NET Core) 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 Microsoft.Azure.ServiceBus, and then select the Microsoft.Azure.ServiceBus item. Click Install to complete the installation, then close this dialog box.
Write code to send messages to the queue

In Program.cs, add the following using statements at the top of the namespace definition, before the class declaration,
  1. namespace AzureServiceBus.Console.ServiceBus.SendMessage {  
  2.         using System;  
  3.         using System.Text;  
  4.         using System.Threading;  
  5.         using System.Threading.Tasks;  
  6.         using Microsoft.Azure.ServiceBus;  
  7.         using Newtonsoft.Json.Linq;  
  8.         class Program {  
  9.             const string ServiceBusConnectionString = "Endpoint=sb://xxxxx.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxxxxxxxxxxxxxxx";  
  10.             const string QueueName = "xxxxxxservicebusqueue";  
  11.             static IQueueClient queueClient;  
  12.             static void Main(string[] args) {  
  13.                 MainAsync().GetAwaiter().GetResult();  
  14.             }  
  15.             static async Task MainAsync() {  
  16.                 queueClient = new QueueClient(ServiceBusConnectionString, QueueName);  
  17.                 Console.WriteLine("======================================================");  
  18.                 Console.WriteLine("Press ENTER key to exit after sending all the messages.");  
  19.                 Console.WriteLine("======================================================");  
  20.                 // Send messages.  
  21.                 await SendMessagesAsync(numberOfMessages);  
  22.                 Console.ReadKey();  
  23.                 await queueClient.CloseAsync();  
  24.             }  
  25.             static async Task SendMessagesAsync(int numberOfMessagesToSend) {  
  26.                     try {  
  27.                         // Create a new message to send to the queue  
  28.                         string messageBody = " {\  
  29.                             "glossary\": { \"title\": \"example glossary\",\"GlossDiv\": {\"title\": \"S\",  
  30.                             "GlossList": {  
  31.                                 "GlossEntry": {\  
  32.                                     "ID\": \"SGML\",\"SortAs\": \"SGML\",\"GlossTerm\": \"Standard Generalized Markup Language\",\"Acronym\": \"SGML\",\  
  33.                                     "Abbrev\": \"ISO 8879:1986\",\"GlossDef\": {\"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",\"GlossSeeAlso\": [\"GML\", \"XML\"]},\"GlossSee\": \"markup\"} } } }}";  
  34.                                     var message = new Message(Encoding.UTF8.GetBytes(messageBody));  
  35.                                     // Write the body of the message to the console.  
  36.                                     Console.WriteLine($ "Sending message: {messageBody}");  
  37.                                     // Send the message to the queue.  
  38.                                     await queueClient.SendAsync(message);  
  39.                                 }  
  40.                                 catch (Exception exception) {  
  41.                                     Console.WriteLine($ "{DateTime.Now} :: Exception: {exception.Message}");  
  42.                                 }  
  43.                             }  
  44.                         }  
  45.                     }