日期:2014-05-19  浏览次数:21010 次

C#如何查看MSMQ里的消息,并能进行删除操作,有人知道吗?
在队列里存放有N条消息,需要能在页面里把这些消息显示出来,并能对这些消息进行删除操作

------解决方案--------------------
帮顶
------解决方案--------------------
/******************************************************************
* Copyright(c) Suzsoft DotNet Group
* Description : MessageQueue helper class
* CreateDate : 2006-05-11 02:28:58
* Creater : QCao
* LastChangeDate:
* LastChanger :
* Version Info :
* ******************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Messaging;

namespace Suzsoft.Alarming.Service
{
/// <summary>
/// Message Queue helper class
/// </summary>
/// <typeparam name= "T "> </typeparam>
public class MessageQueueHelper <T> : MarshalByRefObject where T : class, new()
{
public MessageQueueHelper(string path)
{
m_AllowException = true;
if (MessageQueue.Exists(path))
m_Msq = new MessageQueue(path);
else
{
m_Msq = MessageQueue.Create(path);
m_Msq.MaximumQueueSize = CommonSettings.QueueMaxSize;
}
m_Msq.SetPermissions( "Everyone ", System.Messaging.MessageQueueAccessRights.FullControl);

m_Msq.Formatter = new XmlMessageFormatter(new Type[] { typeof(T) });
m_Msq.Label = typeof(T).Name;
m_Msq.ReceiveCompleted += new ReceiveCompletedEventHandler(Msq_ReceiveCompleted);
}
~MessageQueueHelper()
{
Close();
}
private MessageQueue m_Msq;
private bool m_AllowException;

public bool AllowException
{
get { return m_AllowException; }
set { m_AllowException = value; }
}
private bool MssageQueueReady()
{
if (m_Msq == null)
if (AllowException)
throw new Exception( "The message queue is not ready. ");
else
return false;
else
return true;
}
public void Send(object msg)
{
if (!msg.GetType().Equals(typeof(T))) return;
if (!MssageQueueReady()) return;
try
{
m_Msq.Send(msg);
}
catch
{
// TODO: Send Message queue failed
}
}
public void Send(object msg, string label)
{
if (!MssageQueueReady()) return;
try
{
m_Msq.Send(msg, label);
}
catch
{
// TODO: Send Message queue failed
}
}
public T Receive()
{
if (!MssageQueueReady()) return default(T);
Message m = m_Msq.Receive(MessageQueueTransactionType.Single);
return m.Body as T;
}
public IList <T> ReceiveAll()
{
if (!MssageQueueReady()) return null;
Message[] ms = m_Msq.GetAllMessages();
IList <T> list = new List <T> ();
foreach (Message m in ms)
{