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);
}
Cursor
. It appears to only inherit one fromObject
. However, if that were the case, the GC shouldn't actually invoke it at all. Is the source available? – LiddellCreateCursor
anywhere in your code? – LiddellSystem.Messaging.Cursor
. Both ILSpy and .NET Reflector indicate there's no finalizer for this class, yet your callstack shows aFinalize
call being the source of the problem. If we could see the content ofSystem.Messaging.Cursor.Finalize
we might be able to identify possible root causes. – Liddell