Jump to content

Accessing MSMQ system queues in C#

Posted on:January 4, 2017 at 01:00 AM

While working with MSMQ you may want to access Dead-Letter Queues. If your messages are not delivered to the expected destination they usually end up in Dead-Letter Queue.

In one of my projects I wanted to be notified if something drop in there. This is how I made up some C# code to monitor Dead-Letter Queue

Queue name

The first problem was to determine what is the queue name! After some google research I ended up with:

string queueName = @"FormatName:DIRECT=OS:.\SYSTEM$;DEADLETTER";

Creepy error

I used same code as in other places in project to access the queue.

Something similar to this:

if (MessageQueue.Exists(queueName))
{
    using (var queue = new MessageQueue(queueName))
    {
        var queueEnum = queue.GetMessageEnumerator2();

        //check if dead message queue has messages
        if (queueEnum.MoveNext())
        {
             //...
             //<notification code here>
        }
    }
}

To my surprise this time I got following exception:

Cannot determine whether a queue with the specified format name exists.

in the first line MessageQueue.Exists(queueName). At first it became quite weird that the queue does not exists. Given that I was able to browse it in “Computer Management Console”.

I tried to search to similar issues. Most of them pointed out permission problems, which was not the case.

Solution

What it turned out is that MessageQueue.Exists(queueName) does not work for system queues. After removing that line from code everything started to work as expected.

Just another developer’s day :)…