Resend to MSMQ after exception

I am trying to return a message to MSMQ when an exception is thrown. The following code will appear, but the message will not be returned to 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);
    }
}
+3
source share
4 answers

A few points: the best way to do this is to use a transaction that spans both queues; this way you know that you will not lose the message.

The second part should be careful about how queues are created and how to send messages in the second place. In particular, MSMQ sometimes seems to “fail” when sending a message (although the error message is actually written elsewhere in the dead letter queue), especially if the transactional send parameters do not match the transactional nature of the queue target.

+5

? , .

+2

I believe you are looking for "Peek" in the post. Use: MessageQueue.Peek, and if you succeed, use this message.

0
source

I managed to get the above code by creating a new queue and specifying the code in the new queue.

Then I compared the 2 queues and noticed that the new queue was multicast (there was no first queue), and the new queue had a label, and the first was not. Otherwise, the queues looked the same.

-1
source

Source: https://habr.com/ru/post/1699381/


All Articles