Receiving messages from the queue

In this section, we will create a console application that will receive messages from the queue. To create this application, follow these steps:

  1. Create a new console application (C#) and name it FlixOne.BookStore.MessageReceiver.
  2. Add the NuGet package for Azure Service Bus (as added in the previous application).
  3. Write the code to receive messages from the Azure Bus Service queue, so your program.cs file contains the method ProcessMessagesAsync():
 static async Task ProcessMessagesAsync(Message message,
CancellationToken token)
{
WriteLine($"Received message: #
{message.SystemProperties.SequenceNumber}
Body:{Encoding.UTF8.GetString(message.Body)}");
await _client.CompleteAsync
(message.SystemProperties.LockToken);
}
  1. Run the application and look at the result. Refer to the following screenshot:
  1. The console window will display the message and its ID. Now, go to the Azure portal and verify the message. It should be zero. Refer to the following screenshot:

The preceding example demonstrates how we can use the Azure Bus Service to send/receive messages for our microservices.