Reputation: 4508
I've got a very simple console app that is having trouble peeking at a message in a remote private queue.
var queues = MessageQueue.GetPrivateQueuesByMachine(machineName);
var queue = queues.Where(x=>x.FormatName == queueName).Single();
Message message = queue.Peek();
The Peek call fails with a MessageQueueException of "Access to Message Queuing system is denied".
Using the same client machine and user I am able to view the queue using Queue Explorer and with the Message Queuing Snap In.
Experimenting with a local queue I am only able to reproduce the error by taking away the Peek permission on the queue but that also stops it in the other tools.
I've seen lots of information that points me to the issues outlined here.
However, it seems like if any of those things was the issue, I wouldn't be able to do it using the other tools as well.
EDIT I have been able to get this to work using the MSMQQueueInfo/MSMQQueue COM objects without changing any credentials. It would be nice if I could make it work using the .NET libraries but at least I have a workaround.
Upvotes: 5
Views: 1573
Reputation: 941237
That the queue shows up in various utilities just doesn't tell you that much. Such a utility is pretty unlikely to be peeking at messages. In general, the default access permissions allows everybody to see the queue and post messages to it. But not retrieve them.
On the machine that owns this queue, use Control Panel > Administrative Tools > Computer Management > Services and Applications > Message Queuing > Private Queues. Select the queue and right click > Properties > Security tab. Note how Everbody has some rights, like "Get Properties" and "Send Message". But not "Peek Message".
Sane thing to do is just add the user account that you use on the other machine and tick the rights you need to get the job done. If this machine is managed by an admin then you'll need to ask them to do it for you.
Upvotes: 2
Reputation: 4508
My issue was that when GetPrivateQueuesByMachine
is used to get the queue, it uses a access mode of SendAndReceive
which is asking for more permissions then I had. I had to use the MessageQueue constructor to specify the AccessMode. (In this case Peek.)
In the end, I was able to get this to work using code similar to the following:
var queue = new MessageQueue(@"FormatName:DIRECT=OS:machineName\private$\queueName", QueueAccessMode.Peek);
Message message = queue.Peek();
Upvotes: 3
Reputation: 645
I too had the same problem. In my case, I was initializing Message Queue in parent thread and accessing Peek function in child thread.
In case you are using multi threading, try to keep initialization and access of function in same thread.
Upvotes: 2