Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 816497 - Last Review: December 3, 2007 - Revision: 2.2
You receive an error message when you perform asynchronous receive operations on a Message Queuing dependent client
You may receive the following error message when you perform asynchronous receive operations on a Microsoft Message Queuing (also known as MSMQ) dependent client:
An unhandled exception of type 'System.NullReferenceException' occurred in Unknown Module.
Additional information: Object reference not set to an instance of an object.
This problem occurs because the
IAsyncResult object that is returned by the
MessageQueue.BeginReceive method is garbage collected before the callback fires.
To resolve this problem, references to the
IAsyncResult object for each invocation of the
BeginReceive method must be kept alive until the callback is complete. If the references are not kept alive, they cannot be garbage collected.
To prevent
IAsyncResult objects from being garbage collected, put them in an
ArrayList class. The following Microsoft Visual Basic .NET code illustrates how to do so:
Public Sub EnableNotifications()
Dim mq as new MessageQueue("server\queue")
AddHandler mq.ReceiveCompleted, AddressOf HandleReceiveCompleted
Dim result as IAsyncResult
result = mq.BeginReceive()
asyncList.Add(result) // asyncList is a global variable of type System.Collections.ArrayList()
End Sub
Public Sub HandleReceiveCompleted(ByVal sender As Object, ByVal e As ReceiveCompletedEventArgs)
Dim m as Message
Dim mq as MessageQueue = CType(sender, MessageQueue)
m = mq.EndReceive(e.AsyncResult)
// Code to handle message goes here.
End Sub
The following Microsoft Visual C# .NET code illustrates how to do so:
public void EnableNotifications()
{
MessageQueue mq = new MessageQueue("server\queue");
mq.ReceiveCompleted += new ReceiveCompletedEventHandler(mq_ReceiveCompleted);
IAsyncResult result = mq.BeginReceive();
asyncList.Add(result); // asyncList is a global variable of type System.Collections.ArrayList()
}
public void mq_ReceiveCompleted(object sender, System.Messaging.ReceiveCompletedEventArgs e)
{
MessageQueue mq = (MessageQueue)sender;
System.Messaging.Message m = mq.EndReceive(e.AsyncResult);
// Code to handle message goes here.
}
APPLIES TO
- Microsoft .NET Framework 1.0
- Microsoft .NET Framework 1.0 Service Pack 1
- Microsoft .NET Framework 1.0 Service Pack 2
- Microsoft .NET Framework 1.0 Service Pack 3
- Microsoft .NET Framework 1.1
- Microsoft Message Queue Server 1.0
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate