Get your hands off my queue

Need exclusive read access to an MSMQ queue? .NET gives you an easy way to do this, but a not-so-obvious way to give up the queue. To obtain a “read lock“ on the queue, simply set MessageQueue.DenySharedReceive to true. From the docs:

DenySharedReceive specifies the shared mode of the queue referenced by this MessageQueue. Set DenySharedReceive to true to indicate that only this MessageQueue should have access to peek or receive messages from the queue with the specified Path. If another MessageQueue or another application is associated with the same queue resource, that instance or application will not be able to peek or receive messages, but it can still send them.

If DenySharedReceive is false, the queue is available to multiple applications for sending, peeking, or receiving messages.

Unfortunately, simply setting DenySharedReceive to false didn't allow others to read from the queue. I had to unsubscribe from ReceiveCompleted and close the queue. YMMV.

No Comments