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

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.

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

 
Prerequisites

Overview of the application


In Part 1, we have created an Azure Service Bus Queue from Azure Portal and also created ASP.NET Core Web API to push the message into that queue,
 
Getting Started With Azure Service Bus Queues And ASP.NET Core Background Services
 
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.
 
Getting Started With Azure Service Bus Queues And ASP.NET Core Background Services
 

Create 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 BackgroundService 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 into service bus. Now we are going to create a hosted background service which is continuously running and listening to service bus messages,
  • Go to solution and right click on it and select Add and select New Project. 
  • Select Console App (.Net Core) and click on Next.

    Getting Started With Azure Service Bus Queues And ASP.NET Core Background Services
  • Give name to service and click on Create.

    Getting Started With Azure Service Bus Queues And ASP.NET Core Background Services 
  •  Create a new file called appsettings.json & add Azure service connection string as below:
    1. {  
    2.  "AppSettings": {   
    3.    "QueueConnectionString""<replace your RootManageSharedAccessKey here>",  
    4.    "QueueName""order-queue"  
    5.  }  
    6. }  
  • First we have to install Azure Service Bus NuGet package:

    Getting Started With Azure Service Bus Queues And ASP.NET Core Background Services 
  • So this service needs to listen to messages from Azure Service bus we can create this as 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:
    1. public class AppSettings  
    2.     {  
    3.         public string QueueConnectionString { getset; }  
    4.         public string QueueName { getset; }  
    5.     }  
  • To configure our service first we need to install the below packages:

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

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

    Getting Started With Azure Service Bus Queues And ASP.NET Core Background Services 
  • After that add the below code in `program.cs` to configure our service,
    1. class Program  
    2.   {  
    3.       static void Main(string[] args)  
    4.       {  
    5.           IServiceCollection serviceDescriptors = new ServiceCollection();  
    6.            Host.CreateDefaultBuilder(args)  
    7.               .UseWindowsService()  
    8.               .ConfigureHostConfiguration(configHost =>  
    9.               {  
    10.                   configHost.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true).Build();  
    11.               })  
    12.               .ConfigureServices((hostContext, services) =>  
    13.               {  
    14.                   var appSettingsConfig = hostContext.Configuration.GetSection(nameof(AppSettings));  
    15.   
    16.                   services.AddOptions();  
    17.                   services.Configure<AppSettings>(appSettingsConfig);  
    18.                   services.AddSingleton(appSettingsConfig);  
    19.               }).Build().Run();  
    20.       }  
    21.   }  
  • 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.
    1. using System;  
    2. using System.Text;  
    3. using System.Threading;  
    4. using System.Threading.Tasks;  
    5. using Microsoft.Azure.ServiceBus;  
    6. using Microsoft.Extensions.Hosting;  
    7. using Microsoft.Extensions.Options;  
    8.   
    9. namespace OrderService  
    10. {  
    11.     public class CraeteOrderHandler : BackgroundService  
    12.     {  
    13.         private readonly AppSettings _appSettings;  
    14.         private IQueueClient _orderQueueClient;  
    15.   
    16.         public CraeteOrderHandler(IOptions<AppSettings> appSettings)  
    17.         {  
    18.             _appSettings = appSettings?.Value ?? throw new ArgumentNullException(nameof(appSettings));  
    19.         }  
    20.   
    21.         public async Task Handle(Message message, CancellationToken cancelToken)  
    22.         {  
    23.             if (message == null)  
    24.                 throw new ArgumentNullException(nameof(message));  
    25.   
    26.             var body = Encoding.UTF8.GetString(message.Body);  
    27.             Console.WriteLine($"Create Order Details are: {body}");  
    28.             await _orderQueueClient.CompleteAsync(message.SystemProperties.LockToken).ConfigureAwait(false);  
    29.         }  
    30.         public virtual Task HandleFailureMessage(ExceptionReceivedEventArgs exceptionReceivedEventArgs)  
    31.         {  
    32.             if (exceptionReceivedEventArgs == null)  
    33.                 throw new ArgumentNullException(nameof(exceptionReceivedEventArgs));  
    34.             return Task.CompletedTask;  
    35.         }  
    36.   
    37.         protected override Task ExecuteAsync(CancellationToken stoppingToken)  
    38.         {  
    39.             var messageHandlerOptions = new MessageHandlerOptions(HandleFailureMessage)  
    40.             {  
    41.                 MaxConcurrentCalls = 5,  
    42.                 AutoComplete = false,  
    43.                 MaxAutoRenewDuration = TimeSpan.FromMinutes(10)  
    44.             };  
    45.             _orderQueueClient = new QueueClient(_appSettings.QueueConnectionString, _appSettings.QueueName);  
    46.             _orderQueueClient.RegisterMessageHandler(Handle, messageHandlerOptions);  
    47.             Console.WriteLine($"{nameof(CraeteOrderHandler)} service has started.");  
    48.             return Task.CompletedTask;  
    49.         }  
    50.   
    51.         public override async Task StopAsync(CancellationToken stoppingToken)  
    52.         {  
    53.             Console.WriteLine($"{nameof(CraeteOrderHandler)} service has stopped.");  
    54.             await _orderQueueClient.CloseAsync().ConfigureAwait(false);  
    55.         }  
    56.   
    57.     }  
    58. }  
  • 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.
    1. 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.

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

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

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