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
- Azure account (If you don't have an Azure subscription, create a free trial account)
- Basic knowledge of Asp.net Core Web API
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.

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.

Create a Background service to listen to the message from the Queue
Prerequisites
- Visual Studio 19(if you are using .NET Core 3.1)
- .NET Core 3.1 SDK installed
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,
- Background task that runs on a timer.
- Hosted service that activates a scoped service. The scoped service can use dependency injection (DI).
- Queued background tasks that run sequentially.
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,
- Go to Solution and right click on it select Add and select New Project.
- Select Console App (.Net Core) and click on Next.

- Give a name to the service and click on Create.

- Create a new file called appsettings.json & add the Azure service connection string as below.
{ "AppSettings": { "QueueConnectionString": "<replace your RootManageSharedAccessKey here>", "QueueName": "order-queue" } } - First, we have to install the Azure Service Bus NuGet package.

- So this service needs to listen to messages from Azure Service bus we can create this as a Windows service. Along with that add the below code to read to the Appsettings from appsettings.json. So create a class called AppSettings.cs as mentioned below.
public class AppSettings { public string QueueConnectionString { get; set; } public string QueueName { get; set; } } - To configure our service first we need to install the below packages.



- After that add the below code in `program. cs` to configure our service.
class Program { static void Main(string[] args) { IServiceCollection serviceDescriptors = new ServiceCollection(); Host.CreateDefaultBuilder(args) .UseWindowsService() .ConfigureHostConfiguration(configHost => { configHost.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true).Build(); }) .ConfigureServices((hostContext, services) => { var appSettingsConfig = hostContext.Configuration.GetSection(nameof(AppSettings)); services.AddOptions(); services.Configure<AppSettings>(appSettingsConfig); services.AddSingleton(appSettingsConfig); }).Build().Run(); } } - Now the next part is to create Background service to listen to messages. So create a class called CraeteOrderHandler.cs and inherit it with class `BackgroundService`. Now override the `ExecuteAsync` and `StopAsync` method of BackgroundService class.
using System; using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.Azure.ServiceBus; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; namespace OrderService { public class CraeteOrderHandler : BackgroundService { private readonly AppSettings _appSettings; private IQueueClient _orderQueueClient; public CraeteOrderHandler(IOptions<AppSettings> appSettings) { _appSettings = appSettings?.Value ?? throw new ArgumentNullException(nameof(appSettings)); } public async Task Handle(Message message, CancellationToken cancelToken) { if (message == null) throw new ArgumentNullException(nameof(message)); var body = Encoding.UTF8.GetString(message.Body); Console.WriteLine($"Create Order Details are: {body}"); await _orderQueueClient.CompleteAsync(message.SystemProperties.LockToken).ConfigureAwait(false); } public virtual Task HandleFailureMessage(ExceptionReceivedEventArgs exceptionReceivedEventArgs) { if (exceptionReceivedEventArgs == null) throw new ArgumentNullException(nameof(exceptionReceivedEventArgs)); return Task.CompletedTask; } protected override Task ExecuteAsync(CancellationToken stoppingToken) { var messageHandlerOptions = new MessageHandlerOptions(HandleFailureMessage) { MaxConcurrentCalls = 5, AutoComplete = false, MaxAutoRenewDuration = TimeSpan.FromMinutes(10) }; _orderQueueClient = new QueueClient(_appSettings.QueueConnectionString, _appSettings.QueueName); _orderQueueClient.RegisterMessageHandler(Handle, messageHandlerOptions); Console.WriteLine($"{nameof(CraeteOrderHandler)} service has started."); return Task.CompletedTask; } public override async Task StopAsync(CancellationToken stoppingToken) { Console.WriteLine($"{nameof(CraeteOrderHandler)} service has stopped."); await _orderQueueClient.CloseAsync().ConfigureAwait(false); } } } - ExecuteAsync() method calls when service starts so we have setup a queue client object and also registered Handle method to listen to messages.
- In StopAsync() method we close the queue client connection.
- In Handle() method we just extract body from messasge which contains our actual data and just print it in console.
- After that let's register our hosted service by adding below line into `ConfigureServices` method in Program.cs class.
services.AddHostedService<CraeteOrderHandler>(); - So let's build and run the service to listen to messages. Start order web api and send message to queue which our OrderService going to listen to.


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!!

Abdul RahimPosted Oct 6, 2022, 12:25 PM
Excellent article, thanks a lot dude :-)
Nadee KariyawasamPosted Jul 21, 2022, 9:11 AM
How to implement the background service and run? In here there implementing part was not included.
Gajo IcPosted Jan 8, 2021, 2:56 AM
Can you please explain how bapckground service works a bit more...I don’t get how it is working automatically (or is it?) if the messages keep coming, how does it know it should handke the incoming messages...or is that code missing here, or automagically part of background service, in which case i’d like to know if this a best practice and would work anywhere (i.e. Linux app service on azure maybe?). TIA