Service Bus queues
Use the following steps to create a Service Bus Queue.
- Create a Service Bus namespace, using the Azure portal.
- Create a Service Bus queue, using the Azure portal.
- Write a .NET console application to send a set of messages to the queue.
- Write a .NET console application to receive those messages from the queue.
Prerequisites
- Visual Studio 2017 or later.
- An Azure subscription.
To create a namespace
- Log on to the Azure portal.
- 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.
- After making sure that the namespace name is available, choose the pricing tier (Basic, Standard, or Premium).
- In the Subscription field, choose an Azure subscription in which to create the namespace.
- In the Resource group field, choose an existing resource group in which the namespace will live, or create a new one.
- In Location, choose the country or region in which your namespace should be hosted.
- 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
- Click All resources, then click the newly created namespace name.
- In the namespace window, click Shared access policies.
- In the Shared access policies screen, click RootManageSharedAccessKey.
- 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.
- 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.
- 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 All services).
- Click the namespace in which you would like to create the queue. In this case, it is sbnstest1.
- In the namespace window, click Queues, then in the Queues window, click + Queue.
- Enter the queue Name and leave the other values with their defaults.
- 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
- Right-click the newly created project and select Manage NuGet Packages.
- 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,
- namespace AzureServiceBus.Console.ServiceBus.SendMessage {
- using System;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using Microsoft.Azure.ServiceBus;
- using Newtonsoft.Json.Linq;
- class Program {
- const string ServiceBusConnectionString = "Endpoint=sb://xxxxx.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxxxxxxxxxxxxxxx";
- const string QueueName = "xxxxxxservicebusqueue";
- static IQueueClient queueClient;
- static void Main(string[] args) {
- MainAsync().GetAwaiter().GetResult();
- }
- static async Task MainAsync() {
- queueClient = new QueueClient(ServiceBusConnectionString, QueueName);
- Console.WriteLine("======================================================");
- Console.WriteLine("Press ENTER key to exit after sending all the messages.");
- Console.WriteLine("======================================================");
-
- await SendMessagesAsync(numberOfMessages);
- Console.ReadKey();
- await queueClient.CloseAsync();
- }
- static async Task SendMessagesAsync(int numberOfMessagesToSend) {
- try {
-
- string messageBody = " {\
- "glossary\": { \"title\": \"example glossary\",\"GlossDiv\": {\"title\": \"S\",
- "GlossList": {
- "GlossEntry": {\
- "ID\": \"SGML\",\"SortAs\": \"SGML\",\"GlossTerm\": \"Standard Generalized Markup Language\",\"Acronym\": \"SGML\",\
- "Abbrev\": \"ISO 8879:1986\",\"GlossDef\": {\"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",\"GlossSeeAlso\": [\"GML\", \"XML\"]},\"GlossSee\": \"markup\"} } } }}";
- var message = new Message(Encoding.UTF8.GetBytes(messageBody));
-
- Console.WriteLine($ "Sending message: {messageBody}");
-
- await queueClient.SendAsync(message);
- }
- catch (Exception exception) {
- Console.WriteLine($ "{DateTime.Now} :: Exception: {exception.Message}");
- }
- }
- }
- }