Receiving MSMQ messages with Windows Service

时光毁灭记忆、已成空白 提交于 2019-11-28 22:09:49

You don't listen. You configure MSMQ Activation to activate your component when messages arrive. 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. 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
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!