0
votes

I have service which listens to Service Bus Queue. Once in a few days I receive MessageLockLostException. Exception itself is clear for me and that's not an issue. However QueueClient seems broken after exception received and stops processing any further messages. I understand that i can add try / catch to ProcessMessagesAsync. However I thought that ExceptionReceivedHandler should handle such cases and QueueClient should try to process same message again(or send it to DLQ if it fails to process it multiple time).

public class QueueService
{
    private readonly QueueClient _queueClient;

    public QueueServiceServiceBus(string connectionString, ILogger<QueueServiceServiceBus> logger)
    {
        _logger = logger;
        var serviceBusConnectionStringBuilder = new ServiceBusConnectionStringBuilder(connectionString);
        _queueClient = new QueueClient(serviceBusConnectionStringBuilder);

        RegisterMessageHandler();
    }

    private Task ExceptionReceivedHandler(ExceptionReceivedEventArgs exceptionReceivedEventArgs)
    {
        _logger.LogError("Message handler encountered an exception {Exception}",
            exceptionReceivedEventArgs.Exception);

        return Task.CompletedTask;
    }        

    private async Task ProcessMessagesAsync(Message message, CancellationToken token)
    {
        //Handle message

        await _queueClient.CompleteAsync(message.SystemProperties.LockToken);
    }

    private void RegisterMessageHandler()
    {
        var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
        {
            AutoComplete = false
        };

        _queueClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
    }
}
1

1 Answers

0
votes

ExceptionReceivedHandler handles the exceptions which occurs during the retrieval and processing of the messages.

The QueueClient will not break for any reason unless it gets throttled with over load.

I tried the sample what you provided. It works fine for me, the ExceptionReceivedHandler handles the MessageLockLostException without breaking.