在C#ASP.NET 3.5 Web應用程序在Windows消息隊列的所有消息,我在一段時間出現以下錯誤一次:零星的錯誤而獲得Windows Server 2003上運行
"Object reference not set to an instance of an object.: at System.Messaging.Interop.MessagePropertyVariants.Unlock()
at System.Messaging.Message.Unlock()
at System.Messaging.MessageQueue.ReceiveCurrent(TimeSpan timeout, Int32 action, CursorHandle cursor, MessagePropertyFilter filter, MessageQueueTransaction internalTransaction, MessageQueueTransactionType transactionType)
at System.Messaging.MessageEnumerator.get_Current()
at System.Messaging.MessageQueue.GetAllMessages()".
的代碼,將引發此行錯誤是:
Message[] msgs = Global.getOutputQueue(mode).GetAllMessages();
其中Global.getOutputQueue(mode)
給我希望從中獲取郵件的消息隊列。
更新:
Global.getPool(mode).WaitOne();
commonClass.log(-1, "Acquired pool: " + mode, "Report ID: " + unique_report_id);
............../* some code / .............. lock(getLock(mode)) { bool yet_to_get = true; int num_retry = 0; do { try { msgs = Global.getOutputQueue(mode).GetAllMessages(); yet_to_get = false; } catch { Global.setOutputQueue(mode); msgs = Global.getOutputQueue(mode).GetAllMessages(); yet_to_get = false; } ++num_retry; } while (yet_to_get && num_retry < 2); } ... / some code*/
....
finally
{
commonClass.log(-1, "Released pool: " + mode, "Report ID: " + unique_report_id);
Global.getPool(mode).Release();
}
Thanks tallseth。 MessageQueue對象已經在Application_Start(Object sender,EventArgs e)中創建。 Global.getOutputQueue(mode)方法只返回它。我錯過了什麼嗎?謝謝。 public static MessageQueue getOutputQueue(char mode) { return(mode =='d')? outputQd:outputQw; } – engg 2012-07-05 22:22:09
聽起來像你可能會遇到相反的問題,一個陳舊的隊列處理。如果可能的話,不要將隊列返回給調用代碼,而是代理您需要的隊列上的方法。然後捕獲此異常,並通過重新創建隊列來處理它,然後再次嘗試。確保不要遞解,但如果事情發生錯誤,你不想要無限循環。 – tallseth 2012-07-05 22:30:35