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);
}
even(bad name) contains? In any case you should use the encoding specified inBasicProperties.ContentEncodingif available, not just assume it's always UTF8. The typical name for event argument isargsbut you can use any name you want. Only the type matters..Bodyincorrectly. What you posted looks like the message header, not the body. What happens if you useBody.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>