前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >原 WCF学习之旅----基础篇之MSMQ

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

作者头像
魂祭心
发布2018-05-17 15:59:01
4500
发布2018-05-17 15:59:01
举报
文章被收录于专栏:魂祭心魂祭心魂祭心

服务端

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);
               }
            }
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档