【发布时间】:2011-12-29 22:57:25
【问题描述】:
我在一个名为“MessageQueueReceive”的类中有这个。
public MessageQueueTransaction BlockingReceive(out Message message)
{
MessageQueueTransaction tran = null;
message = null;
tran = new MessageQueueTransaction();
tran.Begin();
try
{
message = Queue.Receive(new TimeSpan(0, 0, 5), tran);
}
catch (MessageQueueException ex)
{
// If the exception was a timeout, then just continue
// otherwise re-raise it.
if (ex.MessageQueueErrorCode != MessageQueueErrorCode.IOTimeout)
throw ex;
}
return tran;
}
然后我的处理循环有这个:-
while (!Abort)
{
try
{
tran = this.Queue.BlockingReceive(out msg);
if (msg != null)
{
// Process message here
if (tran != null)
tran.Commit();
}
}
catch (Exception ex)
{
if (tran != null)
tran.Abort();
}
}
控制面板工具显示我正在使用的消息队列是事务性的。日志队列未启用。
此代码创建队列:-
private static MessageQueue CreateMessageQueue(string queueName, bool transactional = false)
{
MessageQueue messageQueue = MessageQueue.Create(queueName, transactional);
messageQueue.SetPermissions("Administrators", MessageQueueAccessRights.FullControl,
AccessControlEntryType.Allow);
return messageQueue;
}
调用时事务参数设置为“true”。
我发现,当在消息处理过程中发生异常时,会调用 tran.Abort 但此时我希望消息会返回到队列中。但是,这并没有发生,并且消息丢失了。
我是否遗漏了一些明显的东西?谁能看到我做错了什么?
【问题讨论】:
-
你在本地接收到的队列是监听服务的吗?
-
是的,发送和接收进程和队列都在同一个盒子上。他们是私人队列。我也在几个不同的盒子上运行了相同的代码,并且看到了同样的问题。这表明它与这段代码有关,而不是与机器有关。
-
我猜这个问题很明显。您确定它在中止时遇到异常吗?是否有可能在您的消息处理代码中引发异常,该异常被捕获然后被丢弃,因此它永远不会冒泡到您的异常处理程序中,其中包含中止?
标签: c# .net transactions msmq message-queue