[英]MSMQ first Message.Body in queue is OK, all following Message.Body in queue are empty
我向本地計算機上的MSMQ隊列發送了幾條相同的消息(顯然,Id#除外)。 消息的主體是序列化的XElement對象。
當我嘗試處理隊列中的第一條消息時,我能夠成功地反序列化Message.Body對象並將其保存到文件中。 但是,當嘗試處理下一條(或任何后續)消息時,Message.Body不存在,並且引發了異常。 我已驗證消息ID對於嘗試處理的消息是正確的。
被序列化的XML格式正確。
有任何想法嗎? 我將代碼基於此處找到的Microsoft MSMQ圖書訂購示例: 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();
我在第一次檢索中看到的Message.Body值符合預期: <?xml version="1.0" encoding="utf-8"?> <string>Some String</string>
但是,第二次及其后的檢索操作Message .Body為:“無法反序列化作為參數傳遞的消息。無法識別序列化格式。”
第一次如何運作良好,但之后又如何呢? 我在檢索它后嘗試了message.Dispose(),但沒有幫助。
非常感謝您對此提供的任何幫助!
如果收到第一條消息,然后加載管理控制台以查看隊列,並且可以在屬性對話框中看到這些消息的主體,則可能表明您實際上並未為它們持久保留主體。 也許?
我從來沒有遇到過在MSMQ上進行ID接收的任何問題,盡管我知道在分布式事務下的某些高負載情況下,它確實存在一兩個錯誤,導致線程陷入等待狀態。
然后再次通過ID接收失敗,使隊列失效,所以我從來沒有使用太多:D
也許第二個主體不包含XML序言,因為仍然有一個對象認為它已經編寫了它? 換句話說,您可能需要在某些地方清除某些狀態。
我建議按照kprobst建議在控制台中查看消息。
我可以通過創建一個新對象,然后將序列化的“信封”分配為新對象內部的對象來解決此問題(而不是直接將我的“信封”分配給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");
謝謝大家的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.