Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a System.MessageQueue (MSMQ) message lost if my function fails while processing it?

I send a message using the following code:

var transaction = new MessageQueueTransaction())
transaction.Begin(  );

var message = new Message
{
   Body = myContent,
   Recoverable = true
};

m_oMessageQueue.Send( message , myTransaction );

transaction.Commit( );

And receive it using a BeginRecieve and a ReceiveCompleted event handler.

If my event handler fails before calling EndRecieve, should the message remain on the queue and be available to a subsequent calls to receive? The behavior I am seeing is that the message is gone forever. (Or maybe there is a timeout after which point it will become available again?)

Update The code receiving the message looks like this.

var messageQueue = new MessageQueue( myPath );
messageQueue.ReceiveCompleted += messageQueue_ReceiveCompleted_ThrowException;
messageQueue.BeginReceive();

For test purposes I throw an exception in the messageQueue_ReceiveCompleted_ThrowException event handler.

I then repeat the above code with a working event handler but i doesn't get called.

like image 987
tpower Avatar asked Dec 13 '22 02:12

tpower


1 Answers

It looks like the problem is that you're using BeginReceive with a transactional queue. From MSDN:

Note Do not use the asynchronous call BeginReceive with transactions. If you want to perform a transactional asynchronous operation, call BeginPeek, and put the transaction and the (synchronous) Receive method within the event handler you create for the peek operation. Your event handler might contain functionality as shown in the following C# code.

For failures like this, I believe messages typically go into a dead letter queue or produce a negative acknowledgement, depending on how you configure the MessageQueue (which would explain why you aren't seeing them). You can read more about those options here.

like image 79
Jeff Sternal Avatar answered Dec 16 '22 18:12

Jeff Sternal