The queue does not exist or you do not have sufficient permissions to perform the operation. exception while sending message via MSMQ
Asked Answered
S

1

4

I have created a function to send message via MSMQ but getting exception while executing. below is my function.

public void SendMessageToQueue(ChessQueue chessQueue)
{
    MessageQueue queue = null;
    Message m = null;
    if (!MessageQueue.Exists(".\\Private$\\" + chessQueue.QueueName))
    {
        queue = new MessageQueue(".\\Private$\\chessqueue");
        chessQueue.Messages = new List<MessageObject>();
        chessQueue.Messages.Add(chessQueue.Message);
        queue.Formatter = new BinaryMessageFormatter();
        m = new Message();
        m.Body = chessQueue;
    }
    else
    {
        queue = new MessageQueue(".\\Private$\\" + chessQueue.QueueName);
        queue.Formatter = new BinaryMessageFormatter();
        m = queue.Receive();
        ChessQueue ExistingChessQueue = m.Body as ChessQueue;
        ExistingChessQueue.Messages.Add(chessQueue.Message);
        m.Body = ExistingChessQueue;
    }            
    queue.Send(m);
    // Getting Exception at this Line
}

Exception:- The queue does not exist or you do not have sufficient permissions to perform the operation.

Also I'm unable to open security tab of Messaging Queue under Computer Management. See attached screenshot. enter image description here

I tried creating the message queue under private manually and system allowed me to do so. See below enter image description here

Below is the mmc span in. enter image description here

Slot answered 24/3, 2015 at 5:27 Comment(12)
there appears to be a problem with your Windows user account profile perhaps. did you try creating another user account and then creating the queue manually? What happens? What about if you uninstall MSMQ, reboot and reinstall it?Jorie
@Ahmedilyas: I tried this code on two different machines. MSMQ wans't installed earlier, I installed it and rebooted the system. Still getting the same issue on both machines.Slot
@Ahmedilyas: Just now tried creating a message queue manually and it allows me to create.Slot
@Ahmedilyas: Is it an installation problem? Do i also have to install AD in my system to work with MSMQ?Slot
no, you don't need AD for MSMQ. it is an installation issue.Jorie
@Ahmedilyas: Okay, I am not sure what I did wrong with the installation on both machines. Could you please tell me the correct way of installing MSMQ? I am using Windows 7 ProfessionalSlot
it maybe the machine installation itself. generally you just add from control panel...and that's it. you should be able to create the queue as per normal via MMC snapin like you have shown. did you try a NEW FRESH user account?Jorie
No, not yet. I will try with new user account and let you know.Slot
@Ahmedilyas: I tried it with NEW FRESH user account and getting the same exception.Slot
OK - forget the code for a second. Do you get the error within the MMC snap in? if you do not then ensure that the code has sufficient permissions to create the queue.Jorie
@Ahmedilyas: Yes, I'm able to do so, Please see update in the question. Screenshot attached.Slot
Let us continue this discussion in chat.Slot
M
5
if (!MessageQueue.Exists(".\\Private$\\" + chessQueue.QueueName))
{
    queue = new MessageQueue(".\\Private$\\chessqueue");
    // etc..

There are two bugs in this code. First problem is that it hard-codes the queue name in the string instead of using chessQueue.QueueName. A mismatch will be fatal of course. Second problem, and surely the most critical one, is that it doesn't actually create the queue. Proper code should resemble:

string name = ".\\Private$\\" + chessQueue.QueueName;
if (!MessageQueue.Exists(name))
{
    queue = MessageQueue.Create(name);
    // etc...

Looked like this after I ran this code, with one queue.Send() call:

enter image description here

Meat answered 26/3, 2015 at 12:30 Comment(5)
Thanks for pointing out this silly mistake. I wasn't even creating the queue. Where were you earlier, was waiting for the bounty ;-) :P :D It's now creating the queue, thanks a lot.Slot
But now its stuck in m = queue.Receive(); don't know whySlot
Well, problem solved, congratulations. Now you need to make it communicate with another program, Receive() isn't going to complete until a message appears in the queue. Put there by whatever program you want to talk with.Meat
Message was saved in the queue. I just checked Xml created in the message. After first m.Receive(), message disappears from the queue. Can you tell me some good article to learn MSMQ.Slot
Use Google to find articles, I've been doing this too long and don't know what's "good" anymore. Boilerplate advice is to use WCF instead btw.Meat

© 2022 - 2024 — McMap. All rights reserved.