devlord
devlord

Reputation: 4164

When should I call MessageQueue.EndReceive()?

I have been looking at example code for a service that processes messages from MSMQ. In the code, the EndReceive() method is called right away at the beginning of the ReceiveCompletedEventHandler, and then it begins the task of actually processing the message. Is it just me, or does this totally miss the point of MSMQ reliability? Shouldn't EndReceive() only be called once a message has been fully handled?

Upvotes: 1

Views: 1776

Answers (1)

Jeff Sternal
Jeff Sternal

Reputation: 48583

EndReceive just means that the message was successfully delivered - it doesn't imply anything about whether you were able to do anything worthwhile with it.

It sounds like you're contemplating an asynchronous transactional read from MSMQ, in which you would only finalize the receipt (remove the message from the queue once and for all) upon fully handling your message, whatever that might mean in your context. But asynchronous transactional reads aren't possible - see the MSDN documentation:

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.

Upvotes: 3

Related Questions