QueueClientOptions.MessageDecodingFailed Event

Definition

Optional. Performs the tasks needed when a message is received or peaked from the queue but cannot be decoded.

Such message can be received or peaked when QueueClient is expecting certain QueueMessageEncoding but there's another producer that is not encoding messages in expected way. I.e. the queue contains messages with different encoding.

QueueMessageDecodingFailedEventArgs contains QueueClient that has received the message as well as ReceivedMessage or PeekedMessage with raw body, i.e. no decoding will be attempted so that body can be inspected as has been received from the queue.

The QueueClient won't attempt to remove the message from the queue. Therefore such handling should be included into the event handler itself.

The handler is potentially invoked by both synchronous and asynchronous receive and peek APIs. Therefore implementation of the handler should align with QueueClient APIs that are being used. See SyncAsyncEventHandler<T> about how to implement handler correctly. The example below shows a handler with all possible cases explored.

QueueClientOptions queueClientOptions = new QueueClientOptions()
{
    MessageEncoding = QueueMessageEncoding.Base64
};

queueClientOptions.MessageDecodingFailed += async (QueueMessageDecodingFailedEventArgs args) =>
{
    if (args.PeekedMessage != null)
    {
        Console.WriteLine($"Invalid message has been peeked, message id={args.PeekedMessage.MessageId} body={args.PeekedMessage.Body}");
    }
    else if (args.ReceivedMessage != null)
    {
        Console.WriteLine($"Invalid message has been received, message id={args.ReceivedMessage.MessageId} body={args.ReceivedMessage.Body}");

        if (args.IsRunningSynchronously)
        {
            args.Queue.DeleteMessage(args.ReceivedMessage.MessageId, args.ReceivedMessage.PopReceipt);
        }
        else
        {
            await args.Queue.DeleteMessageAsync(args.ReceivedMessage.MessageId, args.ReceivedMessage.PopReceipt);
        }
    }
};

QueueClient queueClient = new QueueClient(connectionString, queueName, queueClientOptions);
public event Azure.Core.SyncAsyncEventHandler<Azure.Storage.Queues.QueueMessageDecodingFailedEventArgs> MessageDecodingFailed;
member this.MessageDecodingFailed : Azure.Core.SyncAsyncEventHandler<Azure.Storage.Queues.QueueMessageDecodingFailedEventArgs> 
Public Event MessageDecodingFailed As SyncAsyncEventHandler(Of QueueMessageDecodingFailedEventArgs) 

Event Type

Applies to