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

Jitendra Pancholi
  • 7,897
  • 12
  • 51
  • 84
  • 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? – Ahmed ilyas Mar 24 '15 at 06:33
  • @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. – Jitendra Pancholi Mar 24 '15 at 06:36
  • @Ahmedilyas: Just now tried creating a message queue manually and it allows me to create. – Jitendra Pancholi Mar 24 '15 at 06:37
  • @Ahmedilyas: Is it an installation problem? Do i also have to install AD in my system to work with MSMQ? – Jitendra Pancholi Mar 24 '15 at 08:59
  • no, you don't need AD for MSMQ. it is an installation issue. – Ahmed ilyas Mar 24 '15 at 09:36
  • 1
    @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 Professional – Jitendra Pancholi Mar 24 '15 at 10:00
  • 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? – Ahmed ilyas Mar 24 '15 at 14:33
  • No, not yet. I will try with new user account and let you know. – Jitendra Pancholi Mar 24 '15 at 15:02
  • @Ahmedilyas: I tried it with NEW FRESH user account and getting the same exception. – Jitendra Pancholi Mar 25 '15 at 06:30
  • 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. – Ahmed ilyas Mar 25 '15 at 07:57
  • @Ahmedilyas: Yes, I'm able to do so, Please see update in the question. Screenshot attached. – Jitendra Pancholi Mar 25 '15 at 08:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/73771/discussion-between-jitendra-pancholi-and-ahmed-ilyas). – Jitendra Pancholi Mar 25 '15 at 13:38

1 Answers1

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

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 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. – Jitendra Pancholi Mar 26 '15 at 12:50
  • But now its stuck in m = queue.Receive(); don't know why – Jitendra Pancholi Mar 26 '15 at 12:51
  • 1
    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. – Hans Passant Mar 26 '15 at 12:55
  • 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. – Jitendra Pancholi Mar 26 '15 at 12:59
  • 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. – Hans Passant Mar 26 '15 at 13:05