Guy
Guy

Reputation: 67230

Resend to MSMQ after exception

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);
    }
}

Upvotes: 3

Views: 2425

Answers (4)

tomasr
tomasr

Reputation: 13849

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.

Upvotes: 5

Guy
Guy

Reputation: 67230

I managed to get the code above to work by creating a new queue and pointing the code at the new queue.

I then compared the 2 queues and noticed that the new queue was multicast (the first queue wasn't) and the new queue had a label with the first didn't. Otherwise the queues appeared to be the same.

Upvotes: -1

Hans Passant
Hans Passant

Reputation: 941237

Is it really your intention to send that message back to the originator? Sending it back to yourself is very dangerous, you'll just bomb again, over and over.

Upvotes: 2

Gavin Miller
Gavin Miller

Reputation: 43815

I believe that you're looking to "Peek" at the message. Use: MessageQueue.Peek and if you succeed, then consume the message.

Upvotes: 0

Related Questions