This is a continuation of how to utilize Microsoft Message Queuing (MSMQ) in your application. The previous post on the subject is here.
In most cases; the application, service, or web site that reads from an MSMQ will be local to that queue (on the same Windows computer or virtual machine). However; there are exceptions. The one with which I have the most experience is when the MSMQ resource is clustered. In this case, my service is running on one of the nodes of the failover cluster but the Queue is running on the virtual instance of the cluster.
As previously noted, the Microsoft .Net Framework System.Messaging namespace contains the required classes for working with Message Queuing in C#.Net, VB.Net, VBScript, and so forth.
There are several ways to address a remote queue. The following examples use the Direct OS and TCP approaches. There are several other possibilities including sending via HTTP (which is an awesome option but will have to save that for another post).
“FormatName:DIRECT=OS:MyComputer\private$\TestQueue”
“FormatName:DIRECT=TCP:10.200.1.1\private$\TestQueue”
If _messageQueue.QueueName.ToUpper = "NAMEOFQUEUE" Then
If Not _messageQueue.CanRead Then
' Queue permissions not set properly
Return Nothing
End If
Return _messageQueue
End If
Next
The trick to note is that you cannot simply obtain a reference to the queue by name. You must obtain a reference to that machines Queue Collection and then iterate thru that collection until finding the desired queue. You then have a reference to the MessageQueue. Once you have that reference, you may use any of the normal MessageQueue methods (Peek, Receive, ReceiveById, etc…).
Also note the check of the MessageQueue.CanRead property. As previously noted, security/permission is often the thing that causes the most problems in the development sandbox.
For more message queuing implementation details try these additional posts:
Leave a Reply