Cannot determine whether a queue with the specified format name exists

fARcRY picture fARcRY · Dec 16, 2009 · Viewed 17.3k times · Source

I get the exception when executing the following code. Any ideas what is wrong?

string queueName = "FormatName:Direct=TCP:1.1.1.1\\Private$\\test";
MessageQueue queue;

if (MessageQueue.Exists(queueName))
     queue = new System.Messaging.MessageQueue(queueName);
else queue = MessageQueue.Create(queueName);

queue.Send(sWriter.ToString());

Edit: Here is the exception message and first line of stacktrace

Cannot determine whether a queue with the specified format name exists.
at System.Messaging.MessageQueue.Exists(String path)

It works for a local queue by the way.

Answer

Jeff Sternal picture Jeff Sternal · Dec 16, 2009

From your sample, it looks like you're trying to check whether a remote private queue exists, but as the MessageQueue.Exists documentation says:

Exists cannot be called to verify the existence of a remote private queue.

Trying to do so will produce an InvalidOperationException.


If you really need this information for your workflow, you can use the MessageQueue. GetPrivateQueuesByMachine method and iterate the results to find a match. If you do, I recommend reading Are Remote MSMQ Queues Reliable?, which discusses this approach in some depth.

This post from the excellent "MSMQ from the plumber's mate" blog suggests another alternative: don't even check whether your queues exist, "but instead handle the non-delivery of the message should it turn out that the queue doesn't exist." (You'll need to track administration queues and/or dead-letter queues, but you should probably be doing that anyway.)