Given net.msmq Address, How Do I Check if this Queue Exists?
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 messag开发者_运维知识库e?
Edit
The private queues can exist either on the same machine as the service, or on a remote server.
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.
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.
精彩评论