0

I have a service, which inserts stuff into MSMQ via WCF. However, it can be a long time before any messages are inserted into the queue, which is when the error for a non-existing queue would occur.

My idea is to loop through the WCF configuration and checking if the queue exists on startup. The MSMQ WCF endpoints have the address in the format net.msmq://machine/private/queue.

Given a WCF MSMQ address in the net.msmq format, how can I check if this queue exists without queuing an actual message?

Edit

The private queues can exist either on the same machine as the service, or on a remote server.

Mas
  • 4,546
  • 5
  • 39
  • 56

2 Answers2

1

You can check if the queue exists with the MessageQueue.Exists method. Note you have to use the standard MSMQ addressing format which is

FORMATNAME:DIRECT=OS:(server name)\PRIVATE$\(queue name) 

However, checking that a message queue exists before initialising your sender kind of defeats the purpose behind using message queuing in the first place.

One of the reasons you want to use queuing is that your sender does not need to know about the availability of the receiver endpoint. It doesn't care because it can still send the message even if the receiver queue is unavailable.

The reason it can do this is because MSMQ implements a store-and-forward messaging pattern, which means that any sent messages are first stored locally and then sent. This insulates the sender from failures on the destination endpoint and ensures messages won't be lost.

This also means that there will be no error message that the queue address is pointing to a queue which does not exist. The message will simply time out after a configurable time and be marked undeliverable.

UPDATE As John Breakwell points out in his comment: Not so. The MSMQ message will be delivered to the remote queue manager and, if the queue does not exist, be discarded. Negative Source Journaling, if enabled, would put a copy of the discarded message in the sender's corresponding Dead Letter Queue

Hope this helps.

UPDATE

It occurred to me there is a situation where you would want to check for the existence of a queue before sending, which is if you are spawning your queue's programmatically based on some business rule.

tom redfern
  • 30,562
  • 14
  • 91
  • 126
  • The point isn't to check if the queue exists before sending each message. The point is to check if the queue is setup correctly on startup, so I know that something is wrong immediately, rather than some random time in the future. Unfortunately, the MessageQueue.Exists doesn't seem to work for remote private queues. I get the message "System.InvalidOperationException was caught Message=Cannot determine whether a queue with the specified format name exists." – Mas Sep 20 '11 at 10:32
  • Ok, I see what you mean by the second paragraph. I can send a message to a queue that doesn't exist, and no error is thrown. I think I might just abandon this idea completely. – Mas Sep 20 '11 at 10:37
  • I think the error you are getting is because the format of your queue address is incorrect. Also I have updated my answer to expand on a special case where you would want to check the existence of a queue. – tom redfern Sep 20 '11 at 10:46
  • Checking if MSMQ queues exist is hard work so should you bother? (http://blogs.msdn.com/b/johnbreakwell/archive/2008/07/31/checking-if-msmq-queues-exist-is-hard-work-so-should-you-bother.aspx) – John Breakwell Sep 21 '11 at 11:33
  • "This also means that there will be no error message that the queue address is pointing to a queue which does not exist. The message will simply time out after a configurable time and be marked undeliverable." Not so. The MSMQ message will be delivered to the remote queue manager and, if the queue does not exist, be discarded. Negative Source Journaling, if enabled, would put a copy of the discarded message in the sender's corresponding Dead Letter Queue. I don't know how that appears in WCF. – John Breakwell Sep 21 '11 at 11:37
0

You could use the WMI performance counters for MSMQ to check if the queue you are interested in exists. The VBScript code in this post shows the MSMQ WMI objects you'll need to work with. This blog post shows what to do if they're missing on the target machine. You would need to put together a .NET component based on the VBScript code in the link for your service to use.

Sixto Saez
  • 12,610
  • 5
  • 43
  • 51
  • Performance Counters ONLY show data for open queues. That is, queues with >0 messages in or queues held open by an application. Closed, empty queues will not show any perfmon counter data - in fact, they won't even appear in the list of available queues to monitor. – John Breakwell Sep 21 '11 at 11:38