MSMQ first Message.Body in queue is OK, all following Message.Body in queue are empty

Posted by Andrew A on Stack Overflow See other posts from Stack Overflow or by Andrew A
Published on 2010-05-14T18:07:04Z Indexed on 2010/05/14 18:24 UTC
Read the original article Hit count: 566

Filed under:
|
|
|

I send a handful of identical (except for Id#, obviously) messages to an MSMQ queue on my local machine. The body of the messages is a serialized XElement object.

When I try to process the first message in the queue, I am able to successfully de-serialize the Message.Body object and save it to file. However, when trying to process the next (or any subsequent) message, the Message.Body is absent, and an exception is thrown. I have verified the Message ID's are correct for the message attempting to be processed.

The XML being serialized is properly formed.

Any ideas? I am basing my code on the Microsoft MSMQ Book order sample found here: http://msdn.microsoft.com/en-us/library/ms180970%28VS.80%29.aspx

// Create Envelope XML object
        XElement envelope = new XElement(env + "Envelope", new XAttribute(XNamespace.Xmlns + "env", env.NamespaceName) <snip>            

//Send envelope as message body
          MessageQueue myQueue = new MessageQueue(String.Format(@"FORMATNAME:DIRECT=OS:localhost\private$\mqsample"));

          myQueue.DefaultPropertiesToSend.Recoverable = true;

          // Prepare message 
          Message myMessage = new Message();
          myMessage.ResponseQueue = new MessageQueue(String.Format(System.Globalization.CultureInfo.InvariantCulture,
                @"FORMATNAME:DIRECT=TCP:192.168.1.217\private$\mqdemoAck"));
          myMessage.Body = envelope;

          // Send the message into the queue.
          myQueue.Send(myMessage,"message label");

//Retrieve messages from queue
        LabelIdMapping labelID = (LabelIdMapping)mqlistBox3.SelectedItem;
        System.Messaging.Message message = mqOrderQueue.ReceiveById(labelID.Id);

The Message.Body value I see on the 1st retrieve is as expected: <?xml version="1.0" encoding="utf-8"?> <string>Some String</string> However, the 2nd and subsequent retrieve operations Message.Body is: "Cannot deserialize the message passed as an argument. Cannot recognize the serialization format."

How does this work fine the first time but not after that? I have tried message.Dispose() after retrieving it but it did not help.

Thank you very much for any help on this!

© Stack Overflow or respective owner

Related posts about msmq

Related posts about c#