日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

.Net 操作MSMQ

發布時間:2025/4/5 asp.net 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .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;
????????}
????}
}

轉載于:https://www.cnblogs.com/bobofsj11/archive/2009/09/02/1558568.html

總結

以上是生活随笔為你收集整理的.Net 操作MSMQ的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。