簡體   English   中英

使用Windows Service接收MSMQ消息

[英]Receiving MSMQ messages with Windows Service

我正在C#中創建Windows服務。

收聽消息的最佳方法是什么? 我該如何正確編碼?

你不聽 您配置MSMQ激活以在消息到達時激活組件。 該鏈接包含您需要的所有詳細信息,代碼和配置。

如前所述,如果可以使用MSMQ激活,可能是最好的方法。 另外,這是我使用的代碼:

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