Receiving MSMQ messages with Windows Service

前端 未结 2 1399
时光取名叫无心
时光取名叫无心 2020-12-15 01:59

I\'m creating a Windows Service in C#.

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

相关标签:
2条回答
  • 2020-12-15 02:37

    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
      }
    }
    
    0 讨论(0)
  • 2020-12-15 02: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.

    0 讨论(0)
提交回复
热议问题