I’m trying to put a Message back into an MSMQ when an exception is thrown. The following code appears to work but the Message is not put back in the queue?
Message msg = null; try { MessageQueue MQueue = new MessageQueue(txtMsgQPath.Text); msg = MQueue.ReceiveById(txtQItemToRead.Text); lblMsgRead.Text = msg.Body.ToString(); // This line throws exception } catch (Exception ex) { lblMsgRead.Text = ex.Message; if (msg != null) { MessageQueue MQ = new MessageQueue(txtMsgQPath.Text); MQ.Send(msg); } }
Couple of points: The best way to do this would be using a transaction spanning both queues; that way you’ll know you won’t lose a message.
The second part of it is to be careful about how the queues are created and how you submit messages to the second queue. In particular, MSMQ sometimes appears to ‘fail silently’ when sending a message (though in reality an error message is recorded elsewhere in the dead letter queues), particularly if the transactional options of the send don’t match the transactional nature of the target queue.