I am trying to dequeue messages off of an audit
queue that have been put there by the NServiceBus
framework. The messages are Json Serialized and I am using RabbitMQ as a transport layer. As per the tutorials on the RabbitMQ site, I am doing the following:
var factory = new ConnectionFactory { HostName = "localhost" };
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.QueueDeclare("audit", true, false, false, null);
if (myType.IsNormalized()) { }
var consumer = new QueueingBasicConsumer(channel);
channel.BasicConsume("audit", true, consumer);
while (true)
{
var eventArgs = (BasicDeliverEventArgs) consumer.Queue.Dequeue();
var body = eventArgs.Body;
var message = Encoding.UTF8.GetString(body);
var assemblyQualifiedName = String.Concat(eventArgs.BasicProperties.Type,
", Messages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
var objectType = Type.GetType(assemblyQualifiedName);
var deserializedMessage = JsonConvert.DeserializeObject(message, objectType);
}
}
}
This runs fine up until the last line, where it promptly breaks and tells me the JSON is not valid. When I debug and check the content of message, the JSON looks fine to me, however the Visual Studio JSON visualizer claims it is not. What is more bizarre is if I copy the exact content from the debug session and hard code it in, then try to deserialize it, it works fine. If I copy the content into an online JSON validator, it confirms the JSON is valid.
So my question is whether Encoding.UTF8.GetString()
is doing something that can compromise my message
? Is it introducing characters that might not be JSON friendly but could somehow be fixed via copy / paste.
The actual error thrown is: {"Unexpected character encountered while parsing value: . Path '', line 0, position 0."}
EDIT: Below is a sample of the JSON returned by Encoding.UTF8.GetString()
"{\"ProposalKey\":\"123-5\"}"
Corresponding to:
239 187 191 123 34 80 114 111 112 111 115 97 108 75 101 121 34 58 34 49 50 51 45 53 34 125