标签:
服务端
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Messaging; namespace BaseOfWcf { class Program { static void Main(string[] args) { //if (!MessageQueue.Exists(@".\MYFIRSTMSMQ")) //{ // using (MessageQueue mq = MessageQueue.Create(@".\MYFIRSTMSMQ")) // { // mq.Label = "myFirstMSMQ"; // Console.WriteLine("已经创建了一个公共队列{0}", mq.Label); // Console.WriteLine("公共队列{0}的路径为{1}",mq.Label, mq.Path); // mq.Send("MSMQ 消息", "今天又学到个有趣的知识"); // } //} //foreach (MessageQueue mq in MessageQueue.GetPublicQueues()) //{ // mq.Send("发送公共消息队列" + DateTime.Now.ToLongTimeString(), "今天又学到个有趣的知识"); // Console.WriteLine("公有消息已经送到{0}",mq.Path); //} while(true) { Console.WriteLine("*****************"); Console.WriteLine("***0: 创建队列***"); Console.WriteLine("***1: 发送消息***"); Console.WriteLine("***2:清除消息***"); Console.WriteLine("***3: 退出程序***"); Console.WriteLine("*****************"); string s = Console.ReadLine(); switch (s) { case "0": if (!MessageQueue.Exists(@".\Private$\MYFIRSTMSMQ")) { using (MessageQueue mq = MessageQueue.Create(@".\Private$\MYFIRSTMSMQ")) { mq.Label = "myFirstPrivateMSMQ"; Console.WriteLine("已经创建了一个私有队列{0}", mq.Label); Console.WriteLine("私有队列{0}的路径为{1}", mq.Label, mq.Path); mq.Send("MSMQ 私有队列消息" + "今天又学到个有趣的知识"); } } break; case "1": if (MessageQueue.Exists(@".\Private$\MYFIRSTMSMQ")) { MessageQueue mq = new MessageQueue(@".\Private$\MYFIRSTMSMQ"); mq.Send("发送私有消息队列" + DateTime.Now.ToLongTimeString() + "今天又学到个有趣的知识"); Console.WriteLine("私有消息已经送到{0}", mq.Path); } break; case "2": if (MessageQueue.Exists(@".\Private$\MYFIRSTMSMQ")) { MessageQueue mq = new MessageQueue(@".\Private$\MYFIRSTMSMQ"); mq.Purge(); Console.WriteLine("删除成功"); } break; case "3": Environment.Exit(0); break; default: Console.WriteLine("输入有误"); break; } } } } }
客户端
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Messaging; namespace MSMQClient { class Program { static void Main(string[] args) { if (MessageQueue.Exists(@".\Private$\MYFIRSTMSMQ")) { using (MessageQueue mq = new MessageQueue(@".\Private$\MYFIRSTMSMQ")) { mq.Formatter = new XmlMessageFormatter(new string[] {"System.String"}); foreach (Message msg in mq.GetAllMessages()) { Console.WriteLine("接收到的消息是:{0}",msg.Body); } Message firstmsg = mq.Receive(); Console.WriteLine("收到的第一条消息为:{0}",firstmsg.Body); } } } } }
标签:
原文地址:http://my.oschina.net/u/2403989/blog/503956