.Net中的MessageQueue对象用于与MSMQ消息通信。我被要求现在与MQ交谈。我想重用我下面显示的代码来收听消息。为了做到这一点,我想我需要手动创建一个类似于MessageQueue对象的新对象。
如何创建ReceiveCompleted事件以及BeginRecieve和EndRecieve方法以便与MQ交谈?或者我可以以某种方式使用MQ与.Net?
给出的MessageQueue对象private MessageQueue _OperationMessageQueue;
protected override void OnStart(string[] args)
{
OperationMessageQueue.BeginReceive();
}
protected override void OnStop()
{
OperationMessageQueue.ReceiveCompleted -= Mq_ReceiveCompleted;
OperationMessageQueue.Dispose();
}
protected MessageQueue OperationMessageQueue
{
get
{
if (_OperationMessageQueue == null)
{
_OperationMessageQueue = new MessageQueue(ConfigurationManager.AppSettings["OperationMessageQueuePath"]);
_OperationMessageQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(RequestContainer) });
_OperationMessageQueue.ReceiveCompleted += Mq_ReceiveCompleted;
}
return _OperationMessageQueue;
}
}
private void Mq_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{
//a message has been received
}