.Net 操作MSMQ
生活随笔
收集整理的這篇文章主要介紹了
.Net 操作MSMQ
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Code
下面這個類可以用來直接操作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;
????????}
????}
}
下面這個類可以用來直接操作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;
????????}
????}
}
轉載于:https://www.cnblogs.com/bobofsj11/archive/2009/09/02/1558568.html
總結
以上是生活随笔為你收集整理的.Net 操作MSMQ的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .NET小笔记之程序集
- 下一篇: 基金定投是怎么回事