I am trying to add a string value to a RabbitMQ message.
When I compose the message here is the code:
IBasicProperties props = channel.CreateBasicProperties();
props.ContentType = "text/plain";
props.DeliveryMode = 2;
props.Expiration = "36000000";
props.Headers = new Dictionary<string, object>();
props.Headers.Add("entityId", id);
props.Headers.Add("type", GetMessageType(id));
channel.BasicPublish(exchange: "messages",
routingKey: "",
basicProperties: props,
body: body);
The method GetMessageType returns a string.
I retrieve the message like this:
private void ConsumerOnReceived(object sender, BasicDeliverEventArgs e)
{
var firstMessage = "PO Received";
var secondMessage = "Do you want to print a Back Order Fill Report?";
var id = (int) e.BasicProperties.Headers.FirstOrDefault(x => x.Key == "entityId").Value;
string type;
try
{
type = (string)e.BasicProperties.Headers.FirstOrDefault(x => x.Key == "type").Value;
}
catch (Exception exception)
{
Console.WriteLine(exception);
throw;
}
if (type == "OrderCreated")
{
firstMessage = "Sales Order Created";
secondMessage = "Do you want to print the Order?";
}
...
The first header value (entityId) is retrieved just fine. The second (type) causes an exception.
Exception Message: Message = "Unable to cast object of type 'System.Byte[]' to type 'System.String'."
Do I need to convert the byte array back to a string? Or should I only pass integers?
If I need to convert a byte array back to a string, how do I do that?