我有一个网站,它在远程服务器上使用 MSMQ 对待处理的电子邮件进行排队。我可以将消息写入队列,然后在队列上调用 dispose。队列仍然会收到消息,但稍后 GC 会出现并尝试清理,这会导致 IIS 崩溃。这是我在事件日志中看到的内容:
例外:System.NullReferenceException
消息:未将对象引用设置为对象的实例。
堆栈跟踪:at System.Messaging.Cursor.Finalize()
这段代码多年来一直运行良好,但最近才开始出现问题。我已重新启动所有服务器来排除故障,但这并没有帮助。
Edit 1
这是发送消息的代码。QueueFactory
只是一个单例,它有一个围绕创建的锁MessageQueue
.
using (System.Messaging.MessageQueue queue
= QueueFactory.Instance.BuildQueue(this.Path))
{
System.Messaging.Message message = new System.Messaging.Message
{
Body = body,
Formatter = new BinaryMessageFormatter(),
TimeToBeReceived = this.ExpirationMinutes
};
queue.Send(message, label);
}
有一个try-catch
围绕这段代码,我知道它永远不会进入catch
堵塞。我开始认为这是整个应用程序从 .NET 3.5 升级到 .NET 4.0 时造成的。然而,它开始偶尔发生,但现在每次我向队列写入消息时都会发生。
Edit 2
这是代码BuildQueue
lock (lockHandler)
{
return new System.Messaging.MessageQueue(queuePath);
}
尝试使用事务:
using (System.Messaging.MessageQueue queue
= QueueFactory.Instance.BuildQueue(this.Path))
{
System.Messaging.Message message = new System.Messaging.Message
{
Body = body,
Formatter = new BinaryMessageFormatter(),
TimeToBeReceived = this.ExpirationMinutes
};
MessageQueueTransaction transaction = new MessageQueueTransaction();
try
{
transaction.Begin();
queue.Send(message, label, transaction);
transaction.Commit();
}
catch(System.Exception e)
{
transaction.Abort();
throw e;
}
finally
{
transaction.Dispose();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)