码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-09-09 19:53:38      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

服务端

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);
               }
            }
        }
    }
}

技术分享


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

标签:

原文地址:http://my.oschina.net/u/2403989/blog/503956

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!