Windows消息队列(MSMQ)代码

本文介绍如何使用System.Messaging库创建、发送及接收消息队列(MSMQ)。首先展示如何检查队列是否存在,并创建新的队列。接着演示了如何向队列发送消息,最后讲解如何从队列中接收消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引用System.Messaging类库


using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Messaging;

namespace MessageReceiver.App_Code
{
    public class MSMQCommand
    {

        public string Path { get; set; }
        public int Count { get; set; }

        #region 通过Create方法创建使用指定路径的新消息队列 Createqueue(string queuePath)
        /// <summary>
        /// 1.通过Create方法创建使用指定路径的新消息队列
        /// </summary>
        /// <param name="queuePath"></param>
        public void Createqueue(string queuePath)
        {
            try
            {
                if (!MessageQueue.Exists(queuePath))
                {
                    MessageQueue.Create(queuePath);
                    Count = 0;
                }
                //else
                //{
                //    Console.WriteLine(queuePath + "已经存在!");
                //    //MessageQueue.Delete(queuePath);
                //    //MessageQueue.Create(queuePath);
                //    //Console.WriteLine(queuePath + "删除重建");
                //}
                Path = queuePath;
            }
            catch (MessageQueueException e)
            {
                Console.WriteLine(e.Message);
            }
        }
        #endregion

        #region 连接消息队列并发送消息到队列 SendMessage(object sendMessage)
        /// <summary>
        ///  2.连接消息队列并发送消息到队列
        /// 远程模式:MessageQueue rmQ = new MessageQueue("FormatName:Direct=OS:machinename//private$//queue");
        ///     rmQ.Send("sent to regular queue - Atul");对于外网的MSMQ只能发不能收
        /// </summary>
        public void SendMessage(object sendMessage)
        {
            try
            {
                //连接到本地队列
                MessageQueue myQueue = new MessageQueue(Path);
                //MessageQueue myQueue = new MessageQueue("FormatName:Direct=TCP:192.168.12.79//Private$//myQueue1");
                //MessageQueue rmQ = new MessageQueue("FormatName:Direct=TCP:121.0.0.1//private$//queue");--远程格式
                Message myMessage = new Message();
                myMessage.Body = sendMessage;
                myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
                //发生消息到队列中
                myQueue.Send(myMessage);
                Console.WriteLine("消息发送成功!");
                Console.ReadLine();
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }
        }
        #endregion

        #region 连接消息队列并从队列中接收消息 ReceiveMessage()
        /// <summary>
        /// 3.连接消息队列并从队列中接收消息
        /// </summary>
        public void ReceiveMessage()
        {
            MessageQueue myQueue = new MessageQueue(Path);
            myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
            try
            {
                //从队列中接收消息
                Message myMessage = myQueue.Receive();// myQueue.Peek();--接收后不消息从队列中移除
                string context = myMessage.Body.ToString();
                Console.WriteLine("消息内容:" + context);
                Console.ReadLine();
            }
            catch (MessageQueueException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (InvalidCastException e)
            {
                Console.WriteLine(e.Message);
            }
        }
        #endregion
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值