MSMQ - 此计算机上尚未安装消息队列

时间:2012-03-21 03:05:21

标签: msmq

我编写了一个示例应用程序来写入dev服务器上的公共和私有队列。我没有在本地计算机上安装消息队列。

我收到错误:此计算机上尚未安装邮件排队。

错误在这一行:

MessageQueue.Exists(queueName)

以下是完整的测试代码,所有注释且未注释的私有和公共队列都会导致相同的错误。我在这做错了什么?

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;
        }

    }
}

2 个答案:

答案 0 :(得分:18)

在所有想要参与消息传输和接收的计算机上,您需要install MSMQ。这包括在这种情况下发送本地机器等机器。

这是因为MSMQ使用的存储转发消息传递模式。

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

向服务器“发送”消息时实际发生的是:

  1. 本地队列管理器将消息写入本地临时队列。
  2. 本地队列管理器连接到远程队列管理器。
  3. 传输消息。
  4. 远程队列管理器将消息写入远程队列。

答案 1 :(得分:5)

将MSMQ逻辑重构为服务并从代码中调用服务,并传递消息。这样你只需要在服务器上安装MSMQ。