Search code examples
.netvb.netmsmq

MSMQ MessageQueueException For a short while after startup


I've got a multi-user planning application where a client sends msmq messages containing their changes to a server application, which processes them, calculates stock/time shortages, and then messages all the connected clients to update their data to show the changes.

This is working pretty well except for one exception that keeps bugging me.

This basically only happens on a few of our faster PC's (with SSD, I7, ...) right after start-up.

ExceptionInformation: System.Messaging.MessageQueueException
Stack:
   at System.Messaging.MessageQueue+MQCacheableInfo.get_ReadHandle()
   at System.Messaging.MessageQueue.StaleSafeReceiveMessage(UInt32, Int32, MQPROPS, System.Threading.NativeOverlapped*, ReceiveCallback, System.Messaging.Interop.CursorHandle, IntPtr)
   at System.Messaging.MessageQueue.ReceiveCurrent(System.TimeSpan, Int32, System.Messaging.Interop.CursorHandle, System.Messaging.MessagePropertyFilter, System.Messaging.MessageQueueTransaction, System.Messaging.MessageQueueTransactionType)
   at System.Messaging.MessageQueue.Receive()
   at Europlanner_.MSMQHandler.ReceiveMessage()
   at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
   at System.Threading.ExecutionContext.runTryCode(System.Object)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ThreadHelper.ThreadStart()

If they open the application a minute or so after boot the error doesn't appear. So although this really isn't a game-breaking bug, I'd like to fix it.

I've done some searches for the exception and I'm guessing it has something to do with permissions, but find it strange that it only happens shortly after boot.

For testing purposes I've tried setting the permissions on the queue so that Everyone and Anonymous Log-on have full control. But that doesn't seem to help.

The machines are all in the same domain, and the queue is on one of our servers in the domain.

The code where the exception occurs is the following:

Private Sub ReceiveMessage()
    While KeepReading
        Dim MSG As Message = ClientQue.Receive()
        If Not DateDiff(DateInterval.Minute, MSG.SentTime, Now) >= 1 Then
            HandleMessage(MSG)
        Else : DebugHandler.WriteLine(DebugTypes.Network, "Removed MSMQ message due to age. (Older than 1 minute") : End If
        LastServerCommunication = 0
    End While
End Sub

My own machine is a little older and slower than the machines of the users, and I personally can't seem to reproduce the bug on my own machine. The time-frame during which I can use a user's machine to test is limited.

Does anyone happen to have an idea as to what is causing this exception?

If I need to post any additional information, just ask.

Thanks for your attention.

Edit to answer Kjell-Åke's question:

I use this little function to check whether the service is started or not:

Private Function QueueServiceStarted() As Boolean
    Dim Services As List(Of ServiceController) = ServiceController.GetServices().ToList
    Dim MSMQueue As ServiceController = Services.Find(Function(o) o.ServiceName = "MSMQ")
    If Not MSMQueue Is Nothing Then If MSMQueue.Status = ServiceControllerStatus.Running Then Return True
    Return False
End Function

Edit to add the exception message:

"Unable to establish connection with Active Directory Domain Services. Verify that   there are sufficient permissions to perform this operation."

Edit: Adding some more info

The application seems to work after the following event in the windows logs.

"The Message Queuing service is online with Active Directory and fully operational."

So basically I'm guessing the question changes to: Why does it take so long for that to happen on our faster PCs.


Solution

  • I've had this problem, end I ended up using an event log trigger and waiting for the MSMQ to get its handshake with the ad server.

    I just abhorred the thought of just thrashing with try / catch / sleep cycle untill it came up. It might be a bit too complex just to fix it, but I found it worth it.

    That way you can structure your code to diffrentiate between startup errors on the client machine and that the MSMQ is just waiting for the AD server.

    Message Queuing Active Directory Operation

    Subscribe to Events in an Event Log