MessageQueueErrorCode -1073741536, Hex Code -0x3FFFFEE0 MSMQ C# .NET 4.0

I got this error on the EndPeek event handler of MSMQ in .NET 4.0 running Visual Studio 2010 on Windows 2003 Server R2. The error shows up as NULL, even though it goes into the MessageQueueException catch. And we find this error code on debugging.

Error receiving/ processing MSMQ messages in QueueReceiver, exception: System.Messaging.MessageQueueException (0x80004005)
   at System.Messaging.MessageQueue.AsynchronousRequest.End()
   at System.Messaging.MessageQueue.EndAsyncOperation(IAsyncResult asyncResult)
   at System.Messaging.MessageQueue.EndPeek(IAsyncResult asyncResult)
   at OnlineAVL.Services.Core.MessageQueueReceiver`1._receiverMessageQueue_PeekCompleted(Object sender, PeekCompletedEventArgs e)

It is more confusing because a simple sample application does not throw this error. I checked everything from project files to compiler and cleaning up code.

After debugging for 2 days, I found that the problem is because BeginPeek() is called in a method which is running on another thread, than the main application. This error happens on both MTA and STA apartment states, but only happens in Win 2003 server.

The same code runs fine in 2008 server (Vista) and above. This is not specific to x86 or x64.

The fix was to check for the OS version and determine whether to run this code in a thread or not. Here is the sample code:

public static bool RunInNewThread()
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                if (Environment.OSVersion.Version.Major > 5)
                    //2003 is 5, Vista and beyond in 6.
                    return true;

            return false;

Popular posts from this blog

Tutorial: Using Google Cloud Storage from C# and .NET

Shocker! - Do not use Environment.MachineName

Enable Visual Studio to use more than 2GB of memory