Message does not reach MSMQ when made transactional

泄露秘密 提交于 2019-12-03 03:11:21

For queue's that you have created as transanctional, you must use the version of Send() that includes the MessageQueueTransactionType parameter. The biggest frustration with this is that it doesn't throw any exceptions or errors as you have seen, but the message just doesn't ever show up.

So, in your code, change:

helpRequestQueue.Send(theMessage); 

to

helpRequestQueue.Send(theMessage, MessageQueueTransactionType.Single); 

Edit: My answer is just another way to do it aside from David's.

Steve Pick

Transactions don't work on non-transactional queues. If you use this form:

using(MessageQueueTransaction tx = new MessageQueueTransaction())
{
    tx.Begin();
    queue.Send(message, tx);
    tx.Commit();
}

On a nontransactional queue, the message appears to be lost and no exception will be thrown. You can check if a queue is transactional in the properties for the queue in the Message Queueing management console.

It's better to use

queue.Send(message, MessageQueueTransactionType.Automatic)

Per MSDN, here's an example of using a transactional MSMQ queue:

    // Connect to a transactional queue on the local computer.
    MessageQueue queue = new MessageQueue(".\\exampleTransQueue");

    // Create a new message.
    Message msg = new Message("Example Message Body");

    // Create a message queuing transaction.
    MessageQueueTransaction transaction = new MessageQueueTransaction();

    try
    {
        // Begin a transaction.
        transaction.Begin();

        // Send the message to the queue.
        queue.Send(msg, "Example Message Label", transaction);

        // Commit the transaction.
        transaction.Commit();
    }
    catch(System.Exception e)
    {
        // Cancel the transaction.
        transaction.Abort();

        // Propagate the exception.
        throw e;
    }
    finally
    {
        // Dispose of the transaction object.
        transaction.Dispose();
    }

You have to treat it like a DB transaction -- begin the transaction by creating the new MSMQ transaction, and then either commit or abort the operation.

Queue and message type need to both be the same - transactional in this case. If you don't get an exception then use Negative Source Journaling in your code to help find lost messages.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!