Andrew Savinykh
Andrew Savinykh

Reputation: 26270

Detecting MSMQ errors

I have this following code (verbatim) that I expect to generate a error:

using System.Messaging;

namespace MsmqTest
{
    class Program
    {
        static void Main()
        {
            string invalidQueue = @"FormatName:DIRECT=OS:sometrahsname\private$\anothertrahsname";
            Enqueue("test",invalidQueue);

        }

        private static void Enqueue(object o, string queueName)
        {
            using (MessageQueue msmq = new MessageQueue(queueName))
            using (MessageQueueTransaction transaction = new MessageQueueTransaction())
            {
                msmq.DefaultPropertiesToSend.Recoverable = true;
                transaction.Begin();
                msmq.Send(new Message(o), transaction);
                transaction.Commit();
            }
        }
    }
}

Here we are sending a message to a queue on a server that does not exist. I expect to receive an indication that something went wrong. I do not get any. How do I check for error in this scenario?

Note: in order to run the code above you need to have MSMQ installed on your machine.

Upvotes: 3

Views: 976

Answers (1)

Ronald Ramos
Ronald Ramos

Reputation: 5460

I stumbled about this problem before in one of my applications. MSDN documented that in MessageQueue.Send, the message might be sent to the dead-letter queue without throwing an exception. And that is what you are experiencing right now. What I did is to check if the queue exists.

using System;
using System.Messaging;

namespace MsmqTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string invalidQueue = @"FormatName:DIRECT=OS:sometrahsname\private$\anothertrahsname";
            Enqueue("test", invalidQueue);
        }

        private static void Enqueue(object o, string queueName)
        {
            try
            {
                MessageQueue msmq = null;
                //check if queueName exists
                //it also validates if you have access to MSMQ server
                if (!MessageQueue.Exists(queueName))
                {
                    msmq = MessageQueue.Create(queueName);

                    //you can also set the permission here
                    //because the other application that may be reading
                    //has different account with the application that created the queue
                    //set to Everyone for demonstration purposes
                    msmq.SetPermissions("Everyone", MessageQueueAccessRights.FullControl);
                }
                else
                {
                    msmq = new MessageQueue(queueName);
                }

                using (msmq)
                {
                    using (MessageQueueTransaction transaction = new MessageQueueTransaction())
                    {
                        msmq.DefaultPropertiesToSend.Recoverable = true;
                        transaction.Begin();
                        msmq.Send(new Message(o), transaction);
                        transaction.Commit();
                    }
                }
            }catch(Exception)
            {
                //handle error here
            }
        }
    }
}

Upvotes: 1

Related Questions