How to: Receive Messages Programmatically

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

You can use a synchronous method called Receive to look at the contents of a queue. When you call the Receive method on a queue, the system removes the first message from the queue and returns it to you. This message is no longer available to other components looking at the queue.

Note

You can also look at the first message on a queue without removing it from the queue. For more information, see How to: Peek at Messages. You can also receive messages asynchronously. For more information, see How to: Receive Messages Asynchronously.

If there are no messages available on the queue when you call the Receive method, the method will wait until a message arrives on the queue. You can specify a time-out period if you want the method to wait for only a specified interval. The time-out period is specified as a TimeSpan object.

When you read messages from a queue, a formatter object is used to serialize and deserialize the message's contents as you manipulate the message. For more information, see Introduction to Reading and Retrieving Messages.

There are several forms of the Receive method:

  • The basic method with several overloads that allow you to specify things such as time-out periods

  • A method called ReceiveById that allows you to retrieve a particular message by its identifier

  • A method called ReceiveByCorrelationId that allows you to retrieve a message by correlation ID

For an explanation of correlation IDs, see Message Queue Journal Storage.

To receive a message programmatically

  1. Create an instance of the MessageQueue component and set its Path property to the queue to which you want to refer. For more information, see How to: Create MessageQueue Component Instances.

  2. Specify the formatter to use to retrieve your message. For more information, see How to: Specify the Formatter for Retrieved Messages.

  3. Create an instance of the Message object to hold the retrieved message.

  4. Call the Receive method to remove the message from the queue.

    Tip

    To specify a time-out for the Receive method, use a TimeSpan object to specify the length of time that you want the method to wait.

    Dim mq As New System.Messaging.MessageQueue(".\MyQueue")
    mq.Send("1", "1")
    Dim m As System.Messaging.Message
    m = mq.Receive(New TimeSpan(0, 0, 3))
    m.Formatter = New System.Messaging.
       XmlMessageFormatter({"System.String,mscorlib"})
    Console.WriteLine(m.Body)
    
            System.Messaging.MessageQueue mq =
               new System.Messaging.MessageQueue(".\\MyQueue");
            mq.Send("1", "1");
            System.Messaging.Message m = mq.Receive(new TimeSpan(0, 0, 3));
            m.Formatter = new System.Messaging.XmlMessageFormatter(new string[] { "System.String,mscorlib" });
            Console.WriteLine(m.Body);
    

See Also

Tasks

How to: Peek at Messages

How to: Receive Messages Asynchronously

How to: Create MessageQueue Component Instances

How to: Specify the Formatter for Retrieved Messages

Other Resources

Reading and Receiving Messages