Jordy
Jordy

Reputation: 345

RabbitMQ C#: How to get dead letter queue header properties (and decode them)

When I want to decode the x-first-dead-queue property of the dead letter, I get the following error:

Argument 1: cannot convert from 'object' to 'byte[]'

This is the code that runs when a new dead letter arrives:

consumer.Received += async (model, ea) =>
            {
                var body = ea.Body.ToArray();
                var message = Encoding.UTF8.GetString(body);

                var originalQueueHeader = ea.BasicProperties.Headers["x-first-death-queue"];
                var originalQueue = Encoding.UTF8.GetString(originalQueueHeader);
            };

Message resolves well, but the originalQueue throws the cannot convert from 'object' to 'byte[]' error.

Upvotes: 0

Views: 1045

Answers (1)

Jordy
Jordy

Reputation: 345

In order to decode the messages, you need to cast the object{byte[]} to a byte[].

So for example, if you want to get the x-first-dead-queue, you need to do the following:

var originalQueueHeader = (byte[])ea.BasicProperties.Headers["x-first-death-queue"];

This way the object{byte[]} will be casted to a byte[].

To get the string as readable text, do the following:

var originalQueue = Encoding.UTF8.GetString(originalQueueHeader);

This can be done for any of the headers. If you want to do this on, for example a numeric header (x-delivery-count), it's as simple as:

var deliveryCount = (long)ea.BasicProperties.Headers["x-delivery-count"];

And it returns the deliveryCount

Upvotes: 1

Related Questions