C# – Testing Whether a Remote MessageQueue Exists (using C#)


How can I tell whether a remote message queue exists? The documentation states that the "Exists" method does not work for remote machines.

The following is not valid (I know the queue path is accurate since I am able to send messages to the queue):

if (!MessageQueue.Exists(@"FormatName:Direct=TCP:\Private$\MyQueue"))
  throw new InvalidOperationException("Queue does not exist");

The problem is that sending a message to a network address that does not have a listening queue behind it does not cause an exception. Having an exception thrown for an invalid queue address is critical to our application.

Best Solution

There is an article about this:

Frank's alternative approach is to make use of other features that MSMQ provides, such as negative acknowledgements messages with administration queues.

What should happen is that either:

  • the message will be delivered successfully to the destination queue
  • a negative acknowledgement (NACK) will be returned to the administration queue with a class of "The destination queue does not exist." (MQMSG_CLASS_NACK_BAD_DST_Q) Alternatively you could use negative source journaling and, on failure to deliver, should see the same class of NACK in the corresponding "Dead-letter messages" system queue.

In summary, don't check if the queue exists but instead handle the non-delivery of the message should it turn out that the queue doesn't exist.


Related Question