Message does not reach MSMQ when made transactional

13,839

Solution 1

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.

Solution 2

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)

Solution 3

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.

Solution 4

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.

Share:
13,839
LCJ
Author by

LCJ

.Net / C#/ SQL Server Developer Some of my posts listed below -- http://stackoverflow.com/questions/3618380/log4net-does-not-write-the-log-file/14682889#14682889 http://stackoverflow.com/questions/11549943/datetime-field-overflow-with-ibm-data-server-client-v9-7fp5/14215249#14215249 http://stackoverflow.com/questions/12420314/one-wcf-service-two-clients-one-client-does-not-work/12425653#12425653 http://stackoverflow.com/questions/18014392/select-sql-server-database-size/25452709#25452709 http://stackoverflow.com/questions/22589245/difference-between-mvc-5-project-and-web-api-project/25036611#25036611 http://stackoverflow.com/questions/4511346/wsdl-whats-the-difference-between-binding-and-porttype/15408410#15408410 http://stackoverflow.com/questions/7530725/unrecognized-attribute-targetframework-note-that-attribute-names-are-case-sen/18351068#18351068 http://stackoverflow.com/questions/9470013/do-not-use-abstract-base-class-in-design-but-in-modeling-analysis http://stackoverflow.com/questions/11578374/entity-framework-4-0-how-to-see-sql-statements-for-savechanges-method http://stackoverflow.com/questions/14486733/how-to-check-whether-postback-caused-by-a-dynamic-link-button

Updated on June 22, 2022

Comments

  • LCJ
    LCJ almost 2 years

    I have a private MSMQ created in my local machine. I am sending messages to the queue using following C# code. When I changed the queue to be transactional, the message is not reaching the MSMQ. However, there is no exception thrown in the Send method. What change I need to make in order to make it working?

    using System;
    using System.Messaging;
    using System.Data;
    
    public partial class _Default : System.Web.UI.Page
    {
        //Sharing violation resulted from queue being open already for exclusive receive.
        MessageQueue helpRequestQueue = new MessageQueue(@".\Private$\MyPrivateQueue", false);
        protected void Page_Load(object sender, EventArgs e)
        {   
            bool isTransactionalQueue = false;    
            if (!System.Messaging.MessageQueue.Exists(@".\Private$\MyPrivateQueue"))    
            {    
                System.Messaging.MessageQueue.Create(@".\Private$\MyPrivateQueue", isTransactionalQueue);    
            }    
            SendMessage();    
            GetAllMessages();    
        }
    
    
        private void SendMessage()    
        {    
            System.Messaging.Message theMessage = new System.Messaging.Message("TimeNow is "+DateTime.Now.ToString());
    
            theMessage.Label = "Lijo " + DateTime.Now.ToString();
    
            theMessage.Priority = System.Messaging.MessagePriority.Normal;
    
            helpRequestQueue.Send(theMessage);    
    
        }
    
    
        private void GetAllMessages()   
        {    
            DataTable messageTable = new DataTable();    
            messageTable.Columns.Add("Label");    
            messageTable.Columns.Add("Body");        
    
    
            //Set Message Filters    
            MessagePropertyFilter filter = new MessagePropertyFilter();    
            filter.ClearAll();    
            filter.Body = true;    
            filter.Label = true;    
            filter.Priority = true;
            helpRequestQueue.MessageReadPropertyFilter = filter;
    
            //Get All Messages    
            System.Messaging.Message[] messages = helpRequestQueue.GetAllMessages();    
            System.Messaging.XmlMessageFormatter stringFormatter = new System.Messaging.XmlMessageFormatter(new string[] { "System.String" });
    
    
            for (int index = 0; index < messages.Length; index++)    
            {    
                string test = System.Convert.ToString(messages[index].Priority);
                messages[index].Formatter = stringFormatter;    
                messageTable.Rows.Add(new string[] {messages[index].Label,messages[index].Body.ToString() });
    
            }
    
    
            Gridview1.DataSource = messageTable;    
            Gridview1.DataBind();    
        }    
    
        private void ReceiveAndProcess()    
        {
    
    
    
        }           
    }