Receiving MSMQ messages with Windows Service

大城市里の小女人 提交于 2019-11-27 14:14:19

问题


I'm creating a Windows Service in C#.

What is the best way to listen for messages?? How do I code this properly??


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/1521841/receiving-msmq-messages-with-windows-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!