The first MSMQ Message.Body in the queue is in order, all the Message.Body queues in the queue are empty
I am posting several identical (except for the Id #, obviously) messages to the MSMQ queue on my local machine. The message body is a serialized XElement object.
When I try to process the first message in the queue, I can successfully de-serialize the Message.Body object and save it to a file. However, when trying to process the next (or any subsequent) message, Message.Body is missing and an exception is thrown. I have verified that the post ID is correct for the message I want to process.
Well-formed XML.
Any ideas? I am basing my code on the Microsoft MSMQ Book order example 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
System.Messaging.Message message = mqOrderQueue.Receive();
The Message.Body value that I see the first time it is exposed looks as expected: <?xml version="1.0" encoding="utf-8"?>
<string>Some String</string>
However, the second and subsequent retrieval operations are Message.Body: "Unable to deserialize message passed as argument. Unable to recognize serialization format."
How does it work the first time, but not after that? I tried message.Dispose () after fetching, but it didn't help.
Thanks a lot for any help with this!
a source to share
I managed to solve this problem by creating a new object and assigning my serialized "envelope" as an object inside the new object (instead of assigning my "envelope" directly to Message.Body.
//Class to create new message object
public class MsgToDB
{
public int someInteger;
public DateTime timeStamp;
public XElement payload;
}
// Create an object of class MsgToDB which will contain envelope object
MsgToDB testMsgObj = new MsgToDB();
testMsgObj.someInteger = 3;
testMsgObj.timeStamp = DateTime.Now;
testMsgObj.payload = envelope;
// Create and send message
Message testMsg = new Message(testMsgObj);
myQueue.Send(testMsg, "message label");
Thanks everyone for the helpful advice.
a source to share
If you get the first message and then load the management console to view the queue and you can see the bodies in the properties dialog for those messages, then this may mean that you never save a body for them. May be?
I've never had any problems getting the receive ID over IDM on MSMQ, although I know that in some high load scenarios in distributed transactions it has an error or two that keep threads waiting.
Then again getting by id sorts the hit point of the queue, so I've never used that much: D
a source to share
Maybe the second body does not contain the XML preamble because the object is still around who thinks it has already written it? In other words, you may have a condition that needs to be cleaned up somewhere.
I recommend looking at the messages in the console as kprobst suggested.
a source to share