OK so this is my first attempt at using a queue really and I'm confident there are some obvious issues here.
This code however has been working locally for me but am having issues having deployed to my test azure environment. At best it runs once but often not at all. The code is being hooked up using:
services.AddHostedService<ServiceBusListener>();
and then this is my main code:
public class ServiceBusListener : BackgroundService
{
private readonly QueueSettings _scoreUpdatedQueueSettings;
private readonly IEventMessageHandler _eventMessageHandler;
private QueueClient _queueClient;
public ServiceBusListener(IOptions<QueueSettings> scoreUpdatedQueueSettings,
IEventMessageHandler eventMessageHandler)
{
_eventMessageHandler = eventMessageHandler;
_scoreUpdatedQueueSettings = scoreUpdatedQueueSettings.Value;
}
public override Task StartAsync(CancellationToken cancellationToken)
{
_queueClient = new QueueClient(_scoreUpdatedQueueSettings.ServiceBusConnectionString,
_scoreUpdatedQueueSettings.QueueName);
var messageHandlerOptions = new MessageHandlerOptions(_eventMessageHandler.ExceptionReceivedHandler)
{
MaxConcurrentCalls = 1,
AutoComplete = false
};
_queueClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
return Task.CompletedTask;
}
public override Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public async Task ProcessMessagesAsync(Message message, CancellationToken token)
{
await _eventMessageHandler.ProcessMessagesAsync(message, token);
await _queueClient.CompleteAsync(message.SystemProperties.LockToken);
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
}
return Task.CompletedTask;
}
}
As mentioned locally this works fine, every time a message hits the queue my relevant functions run.
It feels as though some code should be living in ExecuteAsync but I don't want to create a handler every x do I?
For context this is running on a wep app in azure, I've done it like that as we have an api that can be hit to manage some of the related data.
There seems to be little around this on the net so any help would be appreciated.
TIA