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 beginni开发者_如何学Cng 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?
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.
精彩评论