简体   繁体   English

使用Windows Service接收MSMQ消息

[英]Receiving MSMQ messages with Windows Service

I'm creating a Windows Service in C#. 我正在C#中创建Windows服务。

What is the best way to listen for messages?? 收听消息的最佳方法是什么? How do I code this properly?? 我该如何正确编码?

You don't listen. 你不听 You configure MSMQ Activation to activate your component when messages arrive. 您配置MSMQ激活以在消息到达时激活组件。 The link has all the details you need, code and configuration. 该链接包含您需要的所有详细信息,代码和配置。

As previously stated, MSMQ Activation is probably the best way, if you can use that. 如前所述,如果可以使用MSMQ激活,可能是最好的方法。 Alternatively, here is code that I've used: 另外,这是我使用的代码:

var ts = new TimeSpan(0, 0, 10);
MessageQueue q = GetQueue<T>();
while (true)
{
  try
  {
    Message msg = q.Receive(ts);
    var t = (T)msg.Body;
    HandleMessage(t);
  }
  catch (MessageQueueException e)
  {
    // Test to see if this was just a timeout.
    // If it was, just continue, there were no msgs waiting
    // If it wasn't, something horrible may have happened
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM