Get started with queues
Remarks#
Sample code
More sample code can be found here.
1. Create a namespace using the Azure portal
-
Log on to the Azure classic portal.
-
In the left navigation pane of the portal, click Service Bus.
-
In the lower pane of the portal, click Create.
-
In the Add a new namespace dialog, enter a namespace name. The system immediately checks to see if the name is available.
-
After making sure the namespace name is available, choose the country or region in which your namespace should be hosted.
-
Leave the other fields in the dialog with their default values (Messaging and Standard Tier), then click the OK check mark. 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 credentials
-
In the left navigation pane, click the Service Bus node, to display the list of available namespaces:
-
Select the namespace you just created from the list shown:
-
Click Connection Information.
-
In the Access connection information pane, find the connection string that contains the SAS key and key name.
-
Make a note of the key, or copy it to the clipboard.
2. Create a queue using the Azure portal
-
Log on to the Azure classic portal.
-
In the left navigation pane of the portal, click Service Bus.
-
Select the namespace that you would like to create the queue in. In this case, it is “mytestns1.”
-
Select Queues.
-
Select New in the bottom left corner, then select Quick Create.
-
Enter the Queue Name and ensure the proper namespace is selected.
-
Select Create a new queue.
3. Send messages to the queue
In order 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
-
Right click on the newly created project and select Manage NuGet Packages.
-
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.
Write some code to send a message to the queue
-
Add the following using statement to the top of the Program.cs file.
using Microsoft.ServiceBus.Messaging;
-
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); } } }
-
Run the program, and check the Azure classic portal. Notice that the Queue Length value should now be 1.
4. Receive messages from the queue
-
Create a new console application and add a reference to the Service Bus NuGet package, similar to the sending application above.
-
Add the following
using
statement to the top of the Program.cs file.using Microsoft.ServiceBus.Messaging;
-
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(); } } }
-
Run the program, and check the portal. Notice that the Queue Length value should now be 0.
Congratulations! You have now created a queue, sent a message, and received a message.