Receive MSMQ Messages Using a Windows Service

I am creating a windows service in C #.

What is the best way to listen to messages? How to code this correctly?

+3
source share
2 answers

You are not listening. You configure MSMQ activation to activate your component when messages arrive. The link contains all the necessary information, code and configuration.

+6
source

As said earlier, activating MSMQ is probably the best way if you can use this. In addition, the code I used here is 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
  }
}
+2
source

Source: https://habr.com/ru/post/1719320/


All Articles