17

I have written a sample application to write to a public and private queues that are on dev server. I don't have the message queue installed on my local machine.

I am getting error: message queuing has not been installed on this computer.

Error is on this line:

MessageQueue.Exists(queueName)

Here is the full test code, all commented and not commented private and public queues are resulting in the same error. What am i doing wrong here?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Messaging;

namespace MsmqTest
{
    public partial class Form1 : Form
    {
        //@"DIRECT=OS:devbox01\PRIVATE$\PrivateQueueDev";
        //@"DIRECT=TCP:192.168.6.102\PRIVATE$\PrivateQueueDev";
        private const string QueueName = @"DIRECT=TCP:192.168.6.102\PRIVATE$\PrivateQueueDev";


        //@"DIRECT=OS:devbox01\PublicQueueDev";
        //@"DIRECT=TCP:192.168.6.102\PublicQueueDev";
        private const string QueueNamePublic = @"DIRECT=TCP:192.168.6.102\PublicQueueDev";

        public Form1()
        {
            InitializeComponent();
        }

        private void Write_Click(object sender, EventArgs e)
        {
            MessageQueue msgQ;
            string msgText = String.Format("Message: {0}", DateTime.Now);
            try
            {
                msgQ = GetQ(QueueNamePublic);
                msgQ.Send(msgText);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        private void Read_Click(object sender, EventArgs e)
        {

        }

        private MessageQueue GetQ(string queueName)
        {
            MessageQueue msgQ;

            if(!MessageQueue.Exists(queueName))
            {
                try
                {
                    msgQ = MessageQueue.Create(queueName);
                }
                catch (Exception ex)
                {
                    throw new Exception("Error creating queue", ex);
                }
            }
            else
            {
                try
                {
                    msgQ = new MessageQueue(queueName);
                }
                catch (Exception ex)
                {
                    throw new Exception("Error getting queue", ex);
                }
            }
            return msgQ;
        }

    }
}
learning...
  • 3,104
  • 10
  • 58
  • 96

2 Answers2

22

You need to install MSMQ on ALL machines which want to participate in the transmission and reception of messages. That includes sending machines such as your local machine in this instance.

The reason for this is because of the store-and-forward messaging pattern that MSMQ uses.

http://en.wikipedia.org/wiki/Store_and_forward

What is actually happening when you "send" a message to your server is:

  1. The local queue manager writes the message to a local temporary queue.
  2. The local queue manager connects to the remote queue manager.
  3. The message is transmitted.
  4. The remote queue manager writes the message to the remote queue.
Matthew
  • 1,630
  • 1
  • 14
  • 19
tom redfern
  • 30,562
  • 14
  • 91
  • 126
  • So what are my alternatives here? I don't want to install it on users machine then will be sending the message. Can i use WCF? Could you please point me to some sample code that can help? – learning... Mar 21 '12 at 14:17
  • There is no alternative if you want to use MSMQ. I don't think enabling it on a users machine should be an issue. It's part of windows. – tom redfern Mar 21 '12 at 14:43
  • We have about 15 to 20 users that will need this enabled. What can i use instead of MSMQ? Basically, clients will be sending an id and then listner will need to perform some tasks per that id. It needs to be a queue style process, first in first out. – learning... Mar 21 '12 at 17:33
  • 1
    All the queuing alternatives I know of (RabbitMQ, and ZeroMQ) rely on installations on both ends. Queuing guarantees delivery of each message, exactly once. In order to achieve that you need a client on both ends. If you don't have this requirement then you don't really need to use queuing, and you could use a web service call or something similar. – tom redfern Mar 21 '12 at 19:16
  • How to enable MSMQ (on 64 bit machines): http://www.jprog.com/wiki/How-to-turn-on-Message-Queuing-Server-in-a-64-bit-environment.ashx – John May 21 '12 at 03:24
  • @John You can enable msmq on 64 bit the same way as 32 bit. – tom redfern Sep 11 '12 at 06:49
  • This is a helpful answer. We have 250 desktop users taking install of a package I created that are now going to need MSMQ installed locally, and there do not appear to be many programmatic solutions for doing that. Genius, Microsoft. – Mike K Jun 25 '15 at 14:33
6

Refactor out the MSMQ logic to a service and call the service from your code, passing the message. That way you only have to install MSMQ on the server.