0

I've a rabbitMQ implementation (C# with RabbitMQ.Client 7.1.12 nuget) where we send a short json to a queue. But when the receiver gets the queue item sometimes (approximately 1 on 5) the byte array in the message is just invalid and not what was set on the queue.

When the byte array is invalid it returns something like

\u0001\0\u0001\0\0\0\u001f\02\0\n\0\0\u0013MyTestQueue_123\u0001\0\0\0

But when I check this queue item with RabbitMQ Management, I do see the json I expect. So that makes it even weirder in my eyes.

Sender Code:

...
var msg = new TestClass { i = i, Foo = "1234567890abcdefghijk" };
var msgString = Serializer.Serialize(msg);
var msgBody = System.Text.Encoding.UTF8.GetBytes(msgString);
var msgProps = new BasicProperties
{
    ContentType = "application/json; charset=utf-8",
    CorrelationId = msgGuid.ToString(),
    Headers = new Dictionary<string, object?>()
    {
        { "MessageType", Encoder.Encode(msgTypeName) },
        { "MessageGuid", Encoder.Encode(msgGuid.ToString()) },
    }
};
await channel.BasicPublishAsync(
                        string.Empty,
                        targetName,
                        mandatory: true,
                        msgProps,
                        msgBody);

Receiver code:

private async Task MessageHandler(object sender, BasicDeliverEventArgs @event)
{
   var json = Encoding.UTF8.GetString(@event.Body.Span);
}
3
  • Are you sure that's the correct message? What does the rest of the even (bad name) contains? In any case you should use the encoding specified in BasicProperties.ContentEncoding if available, not just assume it's always UTF8. The typical name for event argument is args but you can use any name you want. Only the type matters. Commented Sep 11 at 10:31
  • BTW you're probably using .Body incorrectly. What you posted looks like the message header, not the body. What happens if you use Body.ToArray() like the doc examples show? ReadOnlyMemory<T> is a view over data owned and handled by the client, not the data itself. There are guidelines around when to use Memory<T> or Span<T> Commented Sep 11 at 10:46
  • If you want to debug you might want to remove the json serialization as a source of errors. You could for example create and send Guids. That should make it fairly easy to compare what was sent and what was received. We recently started using RabbitMQ, and opted to use EasyNetQ on top, since it handles serialization and most of the details of exchanges and queues. Commented Sep 11 at 11:11

1 Answer 1

0

I've found the problem in the code, and is was in a part outside the example code above :( the receiver code was in a Task.Run(async () => but channels aren't thread safe. So that probably caused the error.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.