操作消息队列的简单代码    protected void Button1_Click(object sender, EventArgs e)
    {
        MessageQueue queue = new MessageQueue();
        if (!MessageQueue.Exists(".\\private$\\BookShopOrders"))
        {
            MessageQueue.Create(".\\private$\\BookShopOrders");
        }
        queue.Path = ".\\private$\\BookShopOrders";        queue.Send(TextBox1.Text,MessageQueueTransactionType.Single);
        Label2.Text = "ok";
    }    protected void Button2_Click(object sender, EventArgs e)
    {
        string[] types = { "System.String"};        MessageQueue queue = new MessageQueue();
        queue.Formatter = new XmlMessageFormatter(types);
        
        queue.Path = ".\\private$\\BookShopOrders";
        
        Message message = queue.Receive();
        Label1.Text = (string)message.Body;
    }在专用队列中, 能发现BookShopOrders队列。
第一个方法,即发送方法没有报错,Label2.Text = "ok";显示出来了
但是不论怎么发送, 该队列的消息数目始终为0条
导致怎么也接受不了已经发送的消息。还请各位不吝赐教,拜谢!

解决方案 »

  1.   

    首先,Button1按了以后,你去MQ管理里面看看,这个Queue是不是已经存在了一条信息如果是的话,收消息
    MessageQueue queue = new MessageQueue();
            queue.Formatter = new XmlMessageFormatter(types);
            
            queue.Path = ".\\private$\\BookShopOrders";
    变成
    MessageQueue queue = new MessageQueue(strPath);试试吧
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Messaging; namespace WindowsApplication36
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();        }        void button1_Click(object sender, EventArgs e)
            {
                MessageQueue queue;
                if (!MessageQueue.Exists(".\\private$\\BookShopOrders"))
                    queue = MessageQueue.Create(".\\private$\\BookShopOrders");
                else
                    queue = new MessageQueue(".\\private$\\BookShopOrders");
                queue.Send(TextBox1.Text);
                queue.Close(); 
                Label2.Text = "ok";
            }        void button2_Click(object sender, EventArgs e)
            {
                MessageQueue queue = new MessageQueue(".\\private$\\BookShopOrders");
                queue.Formatter = new XmlMessageFormatter(new String[] { "System.String"});
                System.Messaging.Message message = queue.Receive();
                Label1.Text = Convert.ToString(message.Body);
                queue.Close();
            }
        }
    }