DEV Community

loading...
Cover image for Getting Started with Azure Service Bus Queues & ASP.NET Core - Part 2

Getting Started with Azure Service Bus Queues & ASP.NET Core - Part 2

sumitkharche profile image Sumit Kharche Originally published at c-sharpcorner.com ・4 min read

In this article, we are going to discuss about how to listen message from Azure Service Bus Queues. In the Part 1 we have discussed about Azure Service Bus Queues and how to send message 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 application

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

App Structure

In this article, we are going to create a Background Service to receive a message from Queue.

We will be using same service bus (order-test-asb) and queue (order-queue) that we created in Part 1 for this article.

queue-message

Create Background service to listen 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 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 implements 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 a 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. add-new-project-1
  • Give name to service and click on Create. add-new-project-2
  • Create a new file called appsettings.json & add azure service connection string as below:
{
 "AppSettings": { 
   "QueueConnectionString": "<replace your RootManageSharedAccessKey here>",
   "QueueName": "order-queue"
 }
}
Enter fullscreen mode Exit fullscreen mode
  • First we have to install Azure Service Bus NuGet package: azure-servicebus-nuget-details
  • So this service needs to listen messages from Azure Service bus we can create this as Windows service. Along with that add 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; }
    }
Enter fullscreen mode Exit fullscreen mode
  • To configure our service first we need to install below packages:
    dependency-nuget
    hosting-nuget
    windows-service-nuget

  • After that add 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();
        }
    }
Enter fullscreen mode Exit fullscreen mode
  • Now next part is to create Background service to listen messages. So create a class called CraeteOrderHandler.cs and inherits 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);
        }

    }
}

Enter fullscreen mode Exit fullscreen mode
  • In ExecuteAsync() method calls when service is starts so we have setup a queue client object and also register Handle method to listen 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>();
Enter fullscreen mode Exit fullscreen mode
  • So lets build and run the service to listen messages. Start order web api and send message to queue which our OrderService going to be listen. send-message-using-web-api-postman listen-message-console-log

Conclusion

In this article of Azure Service Bus Queue series, We have learned and implemented Background Service to listen message 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 or LinkedIn

Happy Coding!!

Discussion (4)

pic
Editor guide
Collapse
ankitutekar profile image
Ankit Utekar

Very well written article Sumit!

Collapse
sumitkharche profile image
Collapse
kirkcodes profile image
Kirk Shillingford

I really appreciate this kind of targeted technical article. You did a great job.

Collapse
sumitkharche profile image