【发布时间】:2010-12-04 01:23:17
【问题描述】:
我正在用 C# 创建一个 Windows 服务。
收听消息的最佳方式是什么?我该如何正确编码??
【问题讨论】:
标签: c# windows-services msmq
我正在用 C# 创建一个 Windows 服务。
收听消息的最佳方式是什么?我该如何正确编码??
【问题讨论】:
标签: c# windows-services msmq
你不听。您配置MSMQ Activation 以在消息到达时激活您的组件。该链接包含您需要的所有详细信息、代码和配置。
【讨论】:
如前所述,如果您可以使用,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
}
}
【讨论】:
GetQueue<T>() 方法的下落。