How would I handle poison messages when not using WCF? The code below creates a loop, and I was curious if MSMQ provided a system to automatically handle poison messages.
MessageQueue mq = new MessageQueue(@'.\Private$\My/Queue'); while (true) { using (MessageQueueTransaction _transaction = new MessageQueueTransaction()) { _transaction.Begin(); try { Message msg = mq.Receive(_transaction); //HandleMessage(msg); throw new Exception('Kaboom!'); _transaction.Commit(); } catch (Exception ex) { _transaction.Abort(); } } }
I don’t believe there is a simple way to handle poison messages using raw System.Messaging classes. I think the simplest solution is to set the ‘TimeToBeReceived’ property on a message, but this is not perfect, because you may end up losing valid messages if the receiver is offline. I read somewhere that you can have real poison message handling in MSMQ using PInvoke, but was unable to find any resources on this.
I found this article with some ideas on how to manually handle poison messages. It might offer some ideas:
http://www.cogin.com/articles/SurvivingPoisonMessages.php