手动从.Net库创建事件与MQ交谈

时间:2015-10-05 14:14:11

标签: c# msmq

.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
    }

0 个答案:

没有答案