In this article, we are going to discuss about how to read messages from Azure Service Bus Queues. In Part 1 we have discussed Azure Service Bus Queues and how to send messages into Queues using ASP.NET Core Web API. Please read part 1 before starting this article.

Getting Started With Azure Service Bus Queues And ASP.NET Core

Creating a simple application to send and receive messages from the queue

Prerequisites

Overview of the application

In Part 1, we created an Azure Service Bus Queue from Azure Portal and also created ASP.NET Core Web API to push the message into that queue.

ASP.NET

In this article, we are going to create a Background Service to receive a message from Queue. We will be using the same service bus (order-test-asb) and queue (order-queue) that we created in Part 1 for this article.

Service

Create a Background service to listen to the message from the Queue

Prerequisites

According to the docs,

In ASP.NET Core, background tasks can be implemented as hosted services. A hosted service is a class with background task logic that implements the IHostedService interface. This topic provides three hosted service examples,

Along with IHostedService, we have the Background Service class which is first introduced in ASP.NET Core 3.0 and which is an abstract class that already implements the IHostedService Interface. BackgroundService also gives an abstract method ExecuteAsync() that returns a Task. So in this article, we are inheriting our class with BackgroundService and implementing an ExecuteAsync() method.

In part 1 we have created Order.Web.API which is used to send a message to the service bus. Now we are going to create a hosted background service that is continuously running and listening to service bus messages,

Conclusion

In this Part 2 of Azure Service Bus Queue series, we have learned and implemented Background Service to listen to messages from Azure service bus queue.

I really hope that you enjoyed this article, share it with friends and please do not hesitate to send me your thoughts or comments.

You can follow me on Twitter @sumitkharche01

Happy Coding!!