[英]Windows Non-WCF Service Moving Transacted MSMQ Message to Failed Queue
我有一個在Server 2008上運行的舊版Windows服務,該服務從事務MSMQ隊列讀取消息。 這未配置為WCF服務。
我們希望通過捕獲自定義異常並將相關消息發送到單獨的“失敗”或“毒葯”隊列中,具體取決於拋出的異常類型,從而改進代碼(C#4.0)中失敗和有毒消息的處理。
我無法獲取Catch代碼以將消息發送到單獨的隊列-它從原始隊列中消失(根據需要!),但未顯示在“失敗”隊列中。
為了測試所有隊列,都不需要身份驗證,並且將權限設置為允許所有人執行所有操作。
顯然有什么缺失或錯誤,我懷疑這與交易有關,但我看不到。 也許這是我嘗試做不到的方式?
任何指導/建議表示贊賞!
我們簡化的PeekCompleted事件代碼:
private void MessageReceived(object sender, PeekCompletedEventArgs e)
{
using (TransactionScope txnScope = new TransactionScope())
{
MyMessageType currentMessage = null;
MessageQueue q = ((MessageQueue)sender);
try
{
Message queueMessage = q.EndPeek(e.AsyncResult);
currentMessage = (FormMessage)queueMessage.Body;
Processor distributor = new Processor();
Processor.Process(currentMessage); // this will throw if need be
q.ReceiveById(e.Message.Id);
txnScope.Complete();
q.BeginPeek();
}
catch (MyCustomException ex)
{
string qname = ".\private$\failed";
if (!MessageQueue.Exists(qname)){
MessageQueue.Create(qname , true);
}
MessageQueue fq = new MessageQueue(qname){
Formatter = new BinaryMessageFormatter()
};
System.Messaging.Message message2 = new System.Messaging.Message{
Formatter = new BinaryMessageFormatter(),
Body = currentMessage,
Label = "My Failed Message";
};
fq.Send(message2); //send to failed queue
q.ReceiveById(e.Message.Id); //off of original queue
txnScope.Complete(); // complete the trx
_queue.BeginPeek(); // next or wait
}
//other catches handle any cases where we want to tnxScope.Dispose()
休的以下回答使我們走上了正確的道路。 在Catch塊內,失敗隊列已創建為事務性隊列
MessageQueue.Create(qname , true);
但是發送需要一個TransactionType參數
fq.Send(message2,MessageQueueTransactionType.Single);
做到了。 謝謝休!
如果消息從您的原始隊列中消失,則意味着您的代碼已到達第二個作用域。Complete()(在catch塊中)。
這意味着問題與您發送到錯誤隊列有關。
我建議您需要將隊列創建為事務性隊列,因為您是在范圍內發送的。
MessageQueue fq = new MessageQueue(qname, true){
Formatter = new BinaryMessageFormatter()
};
然后,您需要執行事務發送:
fq.Send(message2, Transaction.Current);
看看是否可行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.