编写 MSMQ 示例应用程序所需的最低限度

我已经研究了一个多小时,找到了如何在 C # 中使用 MSMQ 的很好的例子,甚至找到了一本关于消息队列的书的整整一章... ... 但是对于一个快速测试,我所需要的就是涵盖这个场景,甚至不是以一种完美的方式,只是为了一个快速演示:

“应用程序 A”: 将消息写入消息队列(应用程序 A 是一个 C # 窗口服务) 现在我打开“ ApplicationB”(它是一个 C # winForms 应用程序) ,检查 MSMQ,我看到了一条新消息。

That's it... All I need for a simple demo.

有人能帮我做一个代码示例吗? 非常感谢。

66521 次浏览
//From Windows Service, use this code
MessageQueue messageQueue = null;
if (MessageQueue.Exists(@".\Private$\SomeTestName"))
{
messageQueue = new MessageQueue(@".\Private$\SomeTestName");
messageQueue.Label = "Testing Queue";
}
else
{
// Create the Queue
MessageQueue.Create(@".\Private$\SomeTestName");
messageQueue = new MessageQueue(@".\Private$\SomeTestName");
messageQueue.Label = "Newly Created Queue";
}
messageQueue.Send("First ever Message is sent to MSMQ", "Title");

//From Windows application
MessageQueue messageQueue = new MessageQueue(@".\Private$\SomeTestName");
System.Messaging.Message[] messages = messageQueue.GetAllMessages();


foreach (System.Messaging.Message message in messages)
{
//Do something with the message.
}
// after all processing, delete all the messages
messageQueue.Purge();

对于更复杂的场景,您可以使用 Message 对象来发送消息,在其中包装您自己的类对象,并将您的类标记为可序列化的。还要确保系统上安装了 MSMQ

Perhaps code below will be useful for someone to just get quick intro to MSMQ.

因此,首先我建议你在一个解决方案中创建3个应用程序:

  1. MessageTo (Windows 窗体) Add 1按钮。
  2. MessageFrom (Windows 窗体)添加1个 richtextbox。
  3. MyMessage (类库)添加1个类。

只要复制过去的代码,并尝试它。请确保在您的微软视窗和项目1和2的 安装了 MSMQ有参考 System.Messaging

1. MessageTo (Windows Form) Add 1 button.

using System;
using System.Messaging;
using System.Windows.Forms;


namespace MessageTo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


private void button1_Click(object sender, EventArgs e)
{


#region Create My Own Queue


MessageQueue messageQueue = null;
if (MessageQueue.Exists(@".\Private$\TestApp1"))
{
messageQueue = new MessageQueue(@".\Private$\TestApp1");
messageQueue.Label = "MyQueueLabel";
}
else
{
// Create the Queue
MessageQueue.Create(@".\Private$\TestApp1");
messageQueue = new MessageQueue(@".\Private$\TestApp1");
messageQueue.Label = "MyQueueLabel";
}


#endregion


MyMessage.MyMessage m1 = new MyMessage.MyMessage();
m1.BornPoint = DateTime.Now;
m1.LifeInterval = TimeSpan.FromSeconds(5);
m1.Text = "Command Start: " + DateTime.Now.ToString();


messageQueue.Send(m1);
}
}
}

2. MessageFrom (Windows 窗体)添加1个 richtextbox。

using System;
using System.ComponentModel;
using System.Linq;
using System.Messaging;
using System.Windows.Forms;


namespace MessageFrom
{
public partial class Form1 : Form
{
Timer t = new Timer();
BackgroundWorker bw1 = new BackgroundWorker();
string text = string.Empty;


public Form1()
{
InitializeComponent();


t.Interval = 1000;
t.Tick += T_Tick;
t.Start();


bw1.DoWork += (sender, args) => args.Result = Operation1();
bw1.RunWorkerCompleted += (sender, args) =>
{
if ((bool)args.Result)
{
richTextBox1.Text += text;
}
};
}


private object Operation1()
{
try
{
if (MessageQueue.Exists(@".\Private$\TestApp1"))
{
MessageQueue messageQueue = new MessageQueue(@".\Private$\TestApp1");
messageQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(MyMessage.MyMessage) });




System.Messaging.Message[] messages = messageQueue.GetAllMessages();


var isOK = messages.Count() > 0 ? true : false;
foreach (System.Messaging.Message m in messages)
{
try
{
var command = (MyMessage.MyMessage)m.Body;
text = command.Text + Environment.NewLine;
}
catch (MessageQueueException ex)
{
}
catch (Exception ex)
{
}
}
messageQueue.Purge(); // after all processing, delete all the messages
return isOK;
}
}
catch (MessageQueueException ex)
{
}
catch (Exception ex)
{
}


return false;
}


private void T_Tick(object sender, EventArgs e)
{
t.Enabled = false;


if (!bw1.IsBusy)
bw1.RunWorkerAsync();


t.Enabled = true;
}
}
}

3. MyMessage (类库)添加1个类。

using System;


namespace MyMessage
{
[Serializable]
public sealed class MyMessage
{
public TimeSpan LifeInterval { get; set; }


public DateTime BornPoint { get; set; }


public string Text { get; set; }
}
}

享受:)