In this section, we will create a console application that will receive messages from the queue. To create this application, follow these steps:
- Create a new console application (C#) and name it FlixOne.BookStore.MessageReceiver.
- Add the NuGet package for Azure Service Bus (as added in the previous application).
- 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);
}
- Run the application and look at the result. Refer to the following screenshot:
- 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.