Read Azure Queue Storage from .Net Core Background Task

What is background tasks

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.

How to create a background task

  • Let's continue to add a new class called WeatherDataBackgroundTask in the same solution that we created to make an entry to Azure Queue storage (https://techabhimanyu.blogspot.com/2022/08/azure-storage-queue.htmland try to read the queue data from this background task
  • To make the class WeatherDataBackgroundTask a background task, we need to inherit it from the class BackgroundService and need to implement the method ExecuteAsync
  • We need to run this application for always (as long as the explicit cancel is not requested), so that it can read all entries from the Azure queue storage. We can use while (!stoppingToken.IsCancellationRequested) to achieve the same
  • Let's create a queueClient to connect to queue storage and use queueClient.ReceiveMessageAsync(); to read the message from the queue
  • The method will look like the one below

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            var connectionString = "Your Connection String";
            var queueName = "Your Queue Name";
            var queueClient = new QueueClient(connectionString, queueName);
            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation("Reading from queue");
                var queueMessage = await queueClient.ReceiveMessageAsync();
                if (queueMessage.Value != null)
                {
                    var weatherData = JsonSerializer.Deserialize<WeatherForecast>(queueMessage.Value.MessageText);
                    _logger.LogInformation("New Mesasge Read: {weatherData}", weatherData);
                }
                //Let's wait for 10 seconds to pick the next record from the queue
                await Task.Delay(TimeSpan.FromSeconds(10));
            }
        }

                                          • To use this background service, we need to register it from program.cs file like builder.Services.AddHostedService<WeatherDataBackgroundTask>();
                                          • Now if we run the application, we can see that each of the queue items is picked by the background task every 10 seconds, but the same message reappears in the queue after 30 seconds (default time).
                                          • We need to inform the queue storage that this message is read and no longer needed by writing await queueClient.DeleteMessageAsync(queueMessage.Value.MessageId, queueMessage.Value.PopReceipt);
                                          • We also have another method with the queueClient - called peek which reads the message but don't delete it from the queue.
                                          How to Inject dependency of the Queue client
                                          • We need to register the queueClient from the Program.cs file and add the dependency in the constructor of the controller so that we don't need to create the object every single time when we have to use it.
                                          • Install Nuget package Microsoft.Extensions.Azure which helps us to register different Azure clients in our application
                                          • Inject the dependency from the program.cs file and use the dependency from the constructor

                                          builder.Services.AddAzureClients(builder =>
                                          {
                                              builder.AddClient<QueueClient, QueueClientOptions>((options, _, _) =>
                                              {
                                                  var connectionString = "Your conenction string";
                                                  var queueName = "Your queue name";
                                                  return new QueueClient(connectionString, queueName);
                                              });
                                          });

                                           public WeatherDataBackgroundTask(ILogger<WeatherDataBackgroundTask> logger, QueueClient queueClient)
                                                  {
                                                      _logger = logger;
                                                      _queueClient = queueClient;
                                                  }


                                          References

                                          Source Code: https://github.com/kumarabhimanyu/AzureQueueWithBackgroundTask

                                          Add data to Queue: https://techabhimanyu.blogspot.com/2022/08/azure-storage-queue.html

                                          https://www.youtube.com/watch?v=5oTX6srQdOE&t=2s

                                          https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-6.0&tabs=visual-studio

                                          Comments

                                          Popular posts from this blog

                                          Publish .Net Core Web API to Linux Host

                                          Entity Relationship Using EF Core Code First Approach

                                          Web API Using EF Core