Open existing MSMQ

K

Kimmo Laine

Hi,

one stupid question? How can i be sure that i have valid queue:

using System;
using System.Messaging;

// . . .

MessageQueue q = new MessageQueue( ".\\Private$\\myQueue" );

// Can i call send!?!?
q.Send( "Hello!" );

Is there a way to open existing queue?


thc

Kimmo Laine
 
C

Cezary Nolewajka

You can check a queue existance using the MessageQueue.Exists method.

Then you know whether the queue can be used.
 
K

Kimmo Laine

From MSDN: "Exists is an expensive operation. Use it only when it is
necessary within the application."

Is there another way to do it?


Kimmo Laine


message You can check a queue existance using the MessageQueue.Exists method.

Then you know whether the queue can be used.
 
C

Cezary Nolewajka

True. That is why you should do that only once - when application or service starts.

A trick to use is to do a Peek (TimeSpan.Zero) on the MQ you want to test and see if and what exception (if at all) you receive.

In case of an non-existant queue, the MessageQueueException has the MessageQueueErrorCode property set to "QueueNotFound" value.

If there are no messages in the queue, you will get a timeout exception, so you will know that the queue exists.
If there are any messages, then you will not receive any exceptions.
 
K

Kevin Swanson

I can see that the last message in this thread is almost a month old,
but just in case anyone else is researching this:

The only problem with trying the send and catching the exception is
that a MessageQueueException with the QueueNotFound code is thrown
based on whether the queue is "found" in the directory service. If
you're using a private local queue or an internet queue that's not in
the Message Queuing namespace, you'll get the exception even if your
queue really exists. (see
http://msdn.microsoft.com/library/d...mMessagingMessageQueueErrorCodeClassTopic.asp).

The MessageQueue.Exists method, however, will find your queue
(provided it really really really does exist).

I suggest making the expensive .Exists call once at some point in the
application's life cycle, then catching and ignoring any
MessageQueueExceptions with the code of QueueNotFound
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top