使用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
}
}
如果您使用消息枚举函数而不是接收,那么当队列为空时,您不必担心会发生超时。 – 2009-10-06 17:29:49
非常好咨询!我已经出去了一个星期,我们知道试图回到一个项目是什么样的,但我会尝试一些事情。 Grauenwolf,你能不能请一个Recieve的简短例子?我研究过这一点,但我并没有完全理解它。尤其是在休假一周之后。谢谢.... – 2009-10-16 11:58:21
您可能想提及'GetQueue
是的那是我的第一选择。在这里寻找我的另一个问题。 http://stackoverflow.com/questions/1473179/msmq-not-invoking-com – 2009-10-06 12:06:10
COM激活是MSMQ 3模型。 MSMQ 4有一个基于Windows激活服务和WCF的新激活模型。 – 2009-10-06 14:58:48
我会研究激活服务。 – 2009-10-16 12:06:02