.Net 操作MSMQ

下面这个类可以用来直接操作MSMQ,但有一个需要注意的是,如果你是用APS.NET或WINDOWS SERVICE 操作MSMQ
一定要记的把MSMQ的队列权限设成everyone完全控制,不然会访问不了.我的程序中也增加了对这个权限的控制
mq.SetPermissions("Everyone", MessageQueueAccessRights.FullControl); //这一句就够了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Messaging;
namespace SNET.Common
{
    /// <summary>
    ///
    /// </summary>
    public class MSMQHelper
    {

        /// <summary>
        /// 通过Create方法创建使用指定路径的新消息队列
        /// </summary>
        /// <param name="queuePath"></param>
        public static void Createqueue(string queuePath)
        {
            try
            {
                if (!MessageQueue.Exists(queuePath))
                {
                    MessageQueue mq = MessageQueue.Create(queuePath,true);
                    if (mq != null)
                    {
                        mq.SetPermissions("Everyone", MessageQueueAccessRights.FullControl);
                    }
                }              
            }
            catch (MessageQueueException e)
            {
                throw new Exception(e.ToString());
            }
        }
        /// <summary>
        /// Sends the message.
        /// </summary>
        public static void SendMessage(string queuePath,string strBody)
        {
            MessageQueue myQueue = null;
            try
            {
                //连接到本地的队列
                myQueue = new MessageQueue(queuePath);
                Message myMessage = new Message();
                myMessage.Body = strBody;
                myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
                //发送消息到队列中
                myQueue.Send(myMessage);
                myQueue.Dispose();
            }
            catch (ArgumentException e)
            {
                throw new Exception(e.ToString());
            }
            finally
            {
                if (myQueue != null)
                    myQueue.Dispose();
            }
        }
        /// <summary>
        /// Sends the message.
        /// </summary>
        public static void SendMessage(string queuePath, string queueLable,string strBody)
        {
            MessageQueue myQueue = null;
            try
            {
                //连接到本地的队列
                myQueue = new MessageQueue(queuePath);
                Message myMessage = new Message();
                myMessage.Body = strBody;
                if (queueLable != null)
                {
                    myMessage.Label = queueLable;
                }
                myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
                //发送消息到队列中
                myQueue.Send(myMessage);
                myQueue.Dispose();
            }
            catch (ArgumentException e)
            {
                throw new Exception(e.ToString());
            }
            finally
            {
                if (myQueue != null)
                    myQueue.Dispose();
            }
        }
        /// <summary>
        /// Receives the message.
        /// </summary>
        /// <param name="QueuePath">The queue path.</param>
        /// <returns></returns>
        public static string ReceiveMessage(string QueuePath)
        {            
            //连接到本地队列
            MessageQueue myQueue = new MessageQueue(QueuePath);
            myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
            try
            {
                //从队列中接收消息
                Message myMessage = myQueue.Receive(new TimeSpan(0,0,6));                
                string context = (string)myMessage.Body; //获取消息的内容
                return context;

            }
            catch (MessageQueueException e)
            {
                throw new Exception(e.ToString());
            }
            catch (InvalidCastException e)
            {
                throw new Exception(e.ToString());
            }
            finally
            {
                if (myQueue != null)
                myQueue.Dispose();
            }
            return "";
        }
        /// <summary>
        /// Clears the message.
        /// </summary>
        /// <param name="QueuePath">The queue path.</param>
        public static void ClearAllMessage(string QueuePath)
        {
            MessageQueue myQueue = null;
            try
            {
                myQueue = new MessageQueue(QueuePath);
                myQueue.Purge();
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            finally
            {
                if(myQueue != null)
                myQueue.Dispose();
            }
        }
        /// <summary>
        /// Clears the message.
        /// </summary>
        /// <param name="QueuePath">The queue path.</param>
        public static void DeleteMessage(string QueuePath)
        {          
            try
            {               
                MessageQueue.Delete(QueuePath);
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.ToString());
            }     
        }
        /// <summary>
        /// Gets all message.
        /// </summary>
        /// <param name="QueuePath">The queue path.</param>
        /// <returns></returns>
        public static List<string> GetAllMessage(string QueuePath)
        {
            MessageQueue myQueue = null;
            try
            {
                //连接到本地队列
                myQueue = new MessageQueue(QueuePath);
                Message[] message = myQueue.GetAllMessages();
                XmlMessageFormatter formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
                List<string> msg = new List<string>(message.Length);
                for (int i = 0; i < message.Length; i++)
                {
                    message[i].Formatter = formatter;
                    msg.Add(message[i].Body.ToString());
                }
                return msg;
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            finally
            {
                if (myQueue != null)
                {
                    myQueue.Dispose();
                }
            }
        }
        /// <summary>
        /// Gets all message by enumerator.
        /// </summary>
        /// <param name="QueuePath">The queue path.</param>
        /// <returns></returns>
        public static List<string> GetAllMessageByEnumerator(string QueuePath)
        {
            List<string> msgs = null;
            MessageQueue myQueue = null;
            try
            {
                //连接到本地队列
                myQueue = new MessageQueue(QueuePath);              
                XmlMessageFormatter formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
                MessageEnumerator enumerator = myQueue.GetMessageEnumerator();
                msgs = new List<string>();
                while (enumerator.MoveNext())
                {
                    Message content = (Message)enumerator.Current;
                    content.Formatter = formatter;
                    msgs.Add(content.Body.ToString());
                    enumerator.RemoveCurrent();
                }
                
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            finally
            {
                if (myQueue != null)
                {
                    myQueue.Dispose();
                }
            }
            return msgs;
        }
    }
}

你可能感兴趣的:(.net)