原 WCF学习之旅----基础篇之MSMQ

2018-05-17 15:59:01 浏览数 (1)

服务端

代码语言:javascript复制
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;
                }




            }
        }
    }
}

客户端

代码语言:javascript复制
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);
               }
            }
        }
    }
}

0 人点赞