'Callback or event/delegate from C# core rest api BackgroundService

I´m currently working on a C# core 5.0 REST API with a RabbitMQ messagebus. I´ve created a class that derives from BackgroundService and the consumerEmployeeIncomming.Received event is triggered by messages on the messagebus. But how do I expose the received data to the rest of my application ?

The class is added thru StartUp.cs via: services.AddHostedService<MessageBusSubscriber>();

    public class MessageBusSubscriber : BackgroundService
    {
        private IConfiguration _configuration;
        public MessageBusSubscriber(IConfiguration configuration)
        {
            _configuration = configuration;
            RabbitMqUtil.Initialize(_configuration);
        }
        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            stoppingToken.ThrowIfCancellationRequested();
            if (RabbitMqUtil.IsInitialized)
            {
                var consumerEmployeeIncomming = new EventingBasicConsumer(RabbitMqUtil.EmployeeChannelIncomming);
                consumerEmployeeIncomming.Received += (ModuleHandle, ea) =>
                {
                    Logger.DoLog("CalendarService - MessageBusSubscribe/Employee - event received:");
                    var body = ea.Body;
                    var data = Encoding.UTF8.GetString(body.ToArray());
                    Logger.DoLog(data);
                    List<EmployeePublishDto> list = JsonSerializer.Deserialize<List<EmployeePublishDto>>(data);
                    Logger.DoLog("####################################");
                    Logger.DoLog("Nr of Recieved objects:" + list.Count);
                    foreach (EmployeePublishDto dto in list)
                    {
                        Logger.DoLog(dto.FirstName);
                        Logger.DoLog(dto.LastName);
                        Logger.DoLog(dto.Email);
                    }
                };
                RabbitMqUtil.EmployeeChannelIncomming.BasicConsume(queue: RabbitMqUtil.EmployeeQueueNameIncomming, autoAck: true, consumer: consumerEmployeeIncomming);

If it isn´t possible how do I proceed with using data from RabbitMQ in my application?

Suggestions are greatly appreciated Regards Kaare



Solution 1:[1]

I read through your comment and your code and your comments and think I can shed some light.

I'm still not 100% sure what you're trying to achieve, but I think you're trying to notify the client of changes once the changes have been completed in the queue. The challenge here is the background worker is essentially running in a separate thread. You can't simply "return" the list of employees from a separate thread.

There's a few ways to solve this but I will give you the top two in my opinion.

  1. Use webhooks to notify the client of changes, you can use SignalR to accomplish this. It's pretty easy to set up.

  2. The less pretty way, is you can simply move the queue to a "completed" status, stores the EmployeeList as json to the table, and have a separate endpoint on the client that polls the queue with a check to see if the status is complete, and if so, get the EmployeeList.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Geoffrey Fook