Casey Crookston
Casey Crookston

Reputation: 13945

Can't de-serialize Message.Body when reading from MSMQ

I've seen several other posts here with the same problem, but none offer a solution. And what's really odd is that this works in dev, but not in prod.

I am submitting a message to a queue as follows:

public void QueueMessage(LeadSubmissionMessage message)
    {
        using (var queue = new MessageQueue(MessageQueuePath))
        {
            queue.DefaultPropertiesToSend.Recoverable = true; // always send as recoverable
            queue.Send(message);
        }
    }

This is the LeadSubmissionMessage class:

[Serializable]
public class LeadSubmissionMessage
{
    public long LeadId { get; set; }
    public long UserId { get; set; }
    public DateTime DateTime { get; set; }
}

This is the message, in raw text:

<?xml version="1.0"?>
<LeadSubmissionMessage xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <LeadId>194018</LeadId>
  <UserId>300</UserId>
  <DateTime>2016-05-17T14:52:30.1484784Z</DateTime>
</LeadSubmissionMessage>

That all works fine. But on the receiving end, and only in production, when we do this:

body = message.Body;

It throws this error:

System.InvalidOperationException: Cannot deserialize the message passed as an argument. Cannot recognize the serialization format.
at System.Messaging.XmlMessageFormatter.Read(Message message)
at System.Messaging.Message.get_Body()

It works find in Dev and Staging. I'm trying to minimize and eliminate the points where things could be different, but I've run out of things to check. They are all running the same build version (release). Any MSMQ related config keys match (except for the obvious queue names and locations). One possible variation is the version of MSMQ installed on the machine? But I'm not sure how to check that. Would the OS make a difference? Can't imagine it would.

Upvotes: 2

Views: 2180

Answers (1)

Ivan Yurchenko
Ivan Yurchenko

Reputation: 3871

I'm using it in the following way:

private static MyMessage RecieveMessage()
{
    if (!MessageQueue.Exists(QueueName))
    {
        return null;
    }

    using (var msmq = new MessageQueue(QueueName))
    {
        msmq.Formatter = new XmlMessageFormatter(new Type[] { typeof(MyMessage) });
        var message = msmq.Receive();
        return message != null && message.Body is MyMessage ? (MyMessage)message.Body : null;
    }
}

Not sure what's the problem in your case, but you could try to make it similar to my approach and see if it's working for you.

Upvotes: 2

Related Questions