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.