写一个MSMQ示例应用程序所需的最低 [英] The bare minimum needed to write a MSMQ sample application

查看:113
本文介绍了写一个MSMQ示例应用程序所需的最低的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究了一个多小时,并找到了如何在C#中使用MSMQ很大的样品,甚至有一本关于消息队列整整一章......但对于一个快速测试所有我需要的是覆盖是这种情况,甚至在一个完美的方式,只是为了快速演示:

应用程序A:写一条消息到消息队列。 (申请A是一个C#窗口服务)
现在,我打开应用程序B(这是一个C#WinForms应用程序),我检查MSMQ,我看到哦,我有一个新的消息。

这就是它......我只需要一个简单的演示。

任何人都可以请帮我为这一个code样?大部分AP preciated。


解决方案

  //从Windows服务,使用此code
的MessageQueue的MessageQueue = NULL;
如果(MessageQueue.Exists(@。\\私人$ \\ SomeTestName))
{
    的MessageQueue =新的MessageQueue(@\\私人$ \\ SomeTestName。);
    messageQueue.Label =测试队列;
}
其他
{
    //创建队列
    MessageQueue.Create(@\\私人$ \\ SomeTestName。);
    的MessageQueue =新的MessageQueue(@\\私人$ \\ SomeTestName。);
    messageQueue.Label =新创建的队列
}
messageQueue.Send(第一次消息发送到MSMQ,标题);


  //从Windows应用程序
的MessageQueue的MessageQueue =新的MessageQueue(@\\私人$ \\ SomeTestName。);
System.Messaging.Message [] =的消息messageQueue.GetAllMessages();的foreach(在邮件中System.Messaging.Message消息)
{
    //使用消息的东西。
}
//所有处理后,删除所有信息
messageQueue.Purge();

对于更复杂的情况下,您可以使用Message对象发送消息,包裹里面你自己的类的对象,并且你的类为可序列化。另外要确保MSMQ安装在系统上

I have been researching for over an hour and finding great samples of how to use MSMQ in C# and even one full chapter of a book about Message Queue...But for a quick test all I need is to cover is this scenario, not even in a perfect way, just for a quick demo:

"Application A": Writes a Message to Message Queue. ( Application A is a C# windows service) Now I open "Application B" ( it is a C# winForms app ) and I check MSMQ and I see oh I have a new Message.

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

Could anyone please help me with a code sample for this? Much appreciated.

解决方案

//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();

For more complex scenario, you could use Message objects to send the message, wrap your own class object inside it, and mark your class as serializable. Also be sure that MSMQ is installed on your system

这篇关于写一个MSMQ示例应用程序所需的最低的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆