Reputation: 918
i am trying to deseriliaze a json message coming from MSMQ, but getting exception while parsing.
i am doing like this:
var transaction = new MessageQueueTransaction();
transaction.Begin();
Console.WriteLine("Listening For Message Now...");
var message = queue.Receive(transaction);
var reader = new StreamReader(message.BodyStream,Encoding.Default);
var jsonMessage = reader.ReadToEnd();
var emailMessage = JsonConvert.DeserializeObject<MessageType>(jsonMessage);
The Exception:
................
Json Parser Exception Unexpected character encountered while parsing value: S. Path '', line 0, position 0
The Message Coming from MSMQ with control characters:
.................................
i think this expection is generic expection that comes while parsing.
What is the proper way of receiving the message from the queue?
Would be nice to get some meaningful examples:)
Upvotes: 1
Views: 4801
Reputation: 209
A formatter must be set before and after. Here's what I mean:
MessageQueue mq = new MessageQueue(@".\private$\<YourQueue>");
using (MessageQueueTransaction mqt = new MessageQueueTransaction())
{
mqt.Begin();
message = new Message();
message.Formatter = new XmlMessageFormatter(new Type[] { typeof(String) });
message.Body = JsonConvert.SerializeObject(<YourJsonObject>);
mq.Send(message, mqt);
mqt.Commit();
}
Then you read the message like this:
transaction = new MessageQueueTransaction();
using (MessageQueue mq = new MessageQueue(<YourQueue>)
{
transaction.Begin();
mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(String) });
Message m = mq.Receive(100, transaction);
YourObjectType o = JsonConvert.DeserializeObject<YourObjectType>(m.Body.ToString());
}
transaction.Commit();
Upvotes: 2