Timeout receiving asyncronous message using msmq

Posted on 2006-06-07
Last Modified: 2012-06-21

I'm trying to learn more about messaging and services using an example by Peter Bromberg from Egghead Cafe ( - code can be downloaded from The example runs on a single machine but I've been trying to get it to run with the client and server on seperate machines for about 3 days without luck. I added event log messages at different points and it seems to fall over at the async receive on the server (Invoker.cs in the MSMQInvokerService project):

                                //breaks at this point
                                Message msg=mq.EndReceive(ar);
                                //and goes to the catch
                        ICommand cReceived = (ICommand)msg.Body;
                        ServiceBroker.Logger.WriteToLog("ICommand Object Casted.",
                        ServiceBroker.Logger.WriteToLog("Executed Command at "+DateTime.Now.ToLongTimeString(),

If the service is invoked on the server this means that the message has gotten there, right? What am I doing wrong?

Question by:matt_d_p
    LVL 25

    Expert Comment

    One thing you may try is to post the exception to the event log to see if it actually was a timeout:

                   catch(Exception exception)
                        ServiceBroker.Logger.WriteToLog("An Exception Occured:\n" + exception.ToString(),

    Author Comment

    Thanks for the suggestion. Seems it is a timeout out but why? Why the difference between local and remotes? I'm on a home network with 2 pc's linked by a crossover cable. Shouldn't be any load at all.

    This is the error in the remote log:

    Event Type:      Information
    Event Source:      ServiceBroker
    Event Category:      None
    Event ID:      0
    Date:            2006/06/07
    Time:            09:45:44 PM
    User:            N/A
    Computer:      BIGBLACK
    An Exception Occured:
    System.Messaging.MessageQueueException: Timeout for the requested operation has expired.
       at System.Messaging.MessageQueue.AsynchronousRequest.End()
       at System.Messaging.MessageQueue.EndAsyncOperation(IAsyncResult asyncResult)
       at System.Messaging.MessageQueue.EndReceive(IAsyncResult asyncResult)
       at MSMQInvokerService.Invoker.OnMessageArrival(IAsyncResult ar)

    For more information, see Help and Support Center at

    While playing I've also introduced a bug in the client now:

    FormatName:DIRECT=OS:bigblack\private$\commandtest              //writeline of destinationQueue value
    System.Messaging.MessageQueueException: The specified format name does not suppo
    rt the requested operation. For example, a direct queue format name cannot be de
       at System.Messaging.MessageQueue.SaveQueueProperties()
       at System.Messaging.MessageQueue.set_UseJournalQueue(Boolean value)
       at ClientSample.QueuedClient.QueueInvokerCommand(ICommand c, String destinati
    onQueue) in C:\projects\PosMinder\ClientSample\QueuedClient.cs:line 65

    Could this contribute? It complains about the name format yet still seems to get to the server as I see the errors there. Very confusing?
    LVL 25

    Accepted Solution

    An exception being thrown doesn't necessarily mean that a message was received:

    BeginReceive can specify a time-out, which causes the ReceiveCompleted event to be raised if the time-out occurs before a message appears in the queue. When a time-out occurs without a message arriving in the queue, a subsequent call to EndReceive throws an exception.

    So perhaps the message is not sent after all.

    Author Comment

    If I understand this correctly, there isn't a problem at the server, the server just writes this message each time it "times out" while checking for a message? That explains why I couldn't find anything wrong :). Let me go and find my real problem. Thanks for the help.
    LVL 25

    Expert Comment

    Yes, the ReceivedCompleted event will fire in the event of a timeout, but calling EndReceive if there is no message will cause the exception.  

    You can check the IsCompleted property of the AsyncResult object to see if a message was actually received.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now