MSMQ CreateCursor的NullReferenceException [英] MSMQ CreateCursor NullReferenceException

查看:133
本文介绍了MSMQ CreateCursor的NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用MSMQ的远程服务器上排队待处理电子邮件的网站。我能写的消息队列,然后调用Dispose队列中。队列仍然得到消息,但一段时间后,GC走来,并试图清除,它会导致IIS崩溃。这是我在事件日志中看到:

  

异常: System.NullReferenceException

     

消息:对象引用不设置到对象的实例

     

堆栈跟踪:在System.Messaging.Cursor.Finalize()

这code已被运行正常了多年,但它只是最近开始演戏了。我已经重新启动所有服务器进行故障诊断,但这并不能帮助。

修改1

下面是code正在发送的消息。 QueueFactory 仅仅是一个单例,有围绕创建一个锁 MessageQueue

 使用(System.Messaging.MessageQueue队列
    = QueueFactory.Instance.BuildQueue(this.Path))
{
    System.Messaging.Message消息=新System.Messaging.Message
    {
        身体=身体,
        格式化=新BinaryMessageFormatter()
        TimeToBeReceived = this.ExpirationMinutes
    };

    queue.Send(消息,标签);
}
 

有一个的try-catch 解决此code,我知道它永远不会使它进入阻止。我开始认为这是当整个应用程序进行了升级,从.NET 3.5到.NET 4.0造成的。然而,它开始出现零星的,但现在它发生每当我写一个消息队列的时间。

编辑2

下面是$ C $下 BuildQueue

 锁(lockHandler)
{
    返回新System.Messaging.MessageQueue(queuePath);
}
 

解决方案

请尝试使用一个交易:

 使用(System.Messaging.MessageQueue队列
    = QueueFactory.Instance.BuildQueue(this.Path))
{
    System.Messaging.Message消息=新System.Messaging.Message
    {
        身体=身体,
        格式化=新BinaryMessageFormatter()
        TimeToBeReceived = this.ExpirationMinutes
    };

    MessageQueueTransaction交易=新MessageQueueTransaction();

    尝试
    {
        transaction.Begin();
        queue.Send(邮件,标签,交易);
        器transaction.commit();
    }
    赶上(System.Exception的E)
    {
        transaction.Abort();
        扔ê;
    }
    最后
    {
        transaction.Dispose();
    }
}
 

I have a website that uses MSMQ on a remote server to queue pending e-mails. I am able to write the message to the queue, and then call dispose on the queue. The queue still gets the message, but sometime later the GC comes along and tries to clean up and it causes IIS to crash. This is what I see in the event log:

Exception: System.NullReferenceException

Message: Object reference not set to an instance of an object.

StackTrace: at System.Messaging.Cursor.Finalize()

This code has been running fine for years, but it just started acting up recently. I have rebooting all the servers to troubleshoot it, but that does not help.

Edit 1

Here is the code that is sending the messages. QueueFactory is just a singleton that has a lock around creating a 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);
}

There is a try-catch around this code, and I know it never makes it into the catch block. I'm beginning to think this was caused when the entire application was upgraded from .NET 3.5 to .NET 4.0. However it started occurring sporadically, but now it happens every time I write a message to the queue.

Edit 2

Here is the code for BuildQueue

lock (lockHandler)
{
    return new System.Messaging.MessageQueue(queuePath);
}

解决方案

Try using a transaction:

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();
    }
}

这篇关于MSMQ CreateCursor的NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆