You may receive an "Invalid handle" error message if you remotely read messages or if you remotely send messages by using Message Queuing with a .NET application (842042)



The information in this article applies to:

  • Microsoft Message Queue Server (MSMQ)

SYMPTOMS

If you remotely read messages or if you remotely send messages by using Message Queuing with a .NET application, you may receive the following error message:
Invalid Handle
This error message occurs when all the following conditions are true:
  • You remotely send messages, or you remotely receive messages.
  • You use a Microsoft .NET application with Message Queuing.
  • The Message Queuing service on the remote computer restarts.

CAUSE

This problem occurs because the Microsoft .NET Framework uses a connection cache that contains read handles to message queues or write handles to message queues. By default, this cache is enabled. The message queue borrows handles from the cache every time that the Send method, the Peek method, or the Receive method is called. The message queue borrows handles instead of opening new handles. When you restart the Message Queuing service or when you restart the remote computer, Message Queuing tries to use the queue handle that has been closed. The .NET application may fail, and you may receive the error message that is mentioned in the "Symptoms" section.

WORKAROUND

To work around this problem, follow these steps:
  1. Disable the connection cache by setting the EnableConnectionCache property to False.

    You can use the following sample code to disable the connection cache in Microsoft Visual Basic .NET:
    msgQ.EnableConnectionCache = False
    In the previous sample code, msgQ is an instance of the MessageQueue class in the System.Messaging namespace. System.Messaging is the namespace that contains the set of classes that wrap the underlying Message Queuing infrastructure. The System.Messaging namespace provides classes that permit you to connect to message queues, to monitor message queues, and to administer message queues on the network. The MessageQueue class is the primary object that permits you to interact with message queues on a local computer or on a remote computer.
  2. Clear the cache by calling the ClearConnectionCache method.

    You can use the following sample code to clear the cache:
    msgQ.ClearConnectionCache()
    This previous sample code removes the format names and then closes all the handles that are open in the cache and that are stored in the cache.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to reproduce the problem

To reproduce the problem, follow these steps:
  1. Install Message Queuing on a remote computer.
  2. Create a .NET application that can send messages and that can receive messages from a remote message queue.
  3. Send some messages to the remote message queue by using the MessageQueue.Send method, and then restart either Message Queuing or the remote computer.

    You may receive the error message that is mentioned in the "Symptoms" section.

REFERENCES

For additional information about Message Queuing, visit the following Microsoft Developer Network (MSDN) Web site:For additional information about Message Queuing frequently asked questions, visit the following Microsoft Web site:

Modification Type:MajorLast Reviewed:2/9/2006
Keywords:kbtshoot kbService kbMsg kbprb KB842042 kbAudDeveloper