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;
click below button to copy the code. By azure tutorial team
  • 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();
click below button to copy the code. By azure tutorial team
  • 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();
    }
  }
}
click below button to copy the code. By azure tutorial team
  • Run the program, and check the portal. Notice that the Queue Length value should now be 0.
    • Queue length

Congratulations! You have now created a queue, sent a message, and received a message.


Related Searches to Receive messages from the queue