Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

MessageQueue.Receive Method (MessageQueueTransactionType)

Receives the first message available in the queue referenced by the MessageQueue. This call is synchronous, and blocks the current thread of execution until a message is available.

Namespace:  System.Messaging
Assembly:  System.Messaging (in System.Messaging.dll)

public Message Receive(
	MessageQueueTransactionType transactionType
)

Parameters

transactionType
Type: System.Messaging.MessageQueueTransactionType

One of the MessageQueueTransactionType values, describing the type of transaction context to associate with the message.

Return Value

Type: System.Messaging.Message
A Message that references the first message available in the queue.

ExceptionCondition
MessageQueueException

An error occurred when accessing a Message Queuing method.

InvalidEnumArgumentException

The transactionType parameter is not one of the MessageQueueTransactionType members.

Use this overload to receive a message from a queue using a transaction context defined by the transactionType parameter, or wait until there are messages in the queue.

Specify Automatic for the transactionType parameter if there is already an external transaction context attached to the thread that you want to use to receive the message. Specify Single if you want to receive the message as a single internal transaction. You can specify None if you want to receive a message from a transactional queue outside of a transaction context.

The Receive method allows for the synchronous reading of a message, thereby removing it from the queue. Subsequent calls to Receive will return the messages that follow in the queue.

If this method is called to receive a message from a transactional queue, the message that is received would be returned to the queue if the transaction is aborted. The message is not permanently removed from the queue until the transaction is committed.

To read the first message in a queue without removing it from the queue, use the Peek method. The Peek method always returns the first message in the queue, so subsequent calls to the method return the same message unless a higher priority message arrives in the queue. There is no transaction context associated with a message returned by a call to Peek. Because Peek does not remove any messages in the queue, there would be nothing to roll back by a call to Abort.

Use a call to Receive when it is acceptable for the current thread to be blocked while it waits for a message to arrive in the queue. Because this overload of the Receive method specifies an infinite time-out, the application might wait indefinitely. If the application processing should continue without waiting for the message, consider using the asynchronous method, BeginReceive.

The following table shows whether this method is available in various Workgroup modes.

Workgroup mode

Available

Local computer

Yes

Local computer and direct format name

Yes

Remote computer

No

Remote computer and direct format name

Yes

The following code example demonstrates the use of Receive(MessageQueueTransactionType).


        // Connect to a transactional queue on the local computer.
        MessageQueue queue = new MessageQueue(".\\exampleTransQueue");

        // Create a new message.
        Message msg = new Message("Example Message Body");

        // Send the message.
        queue.Send(msg, MessageQueueTransactionType.Single);

        // Simulate doing other work so the message has time to arrive.
        System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10.0));

        // Set the formatter to indicate the message body contains a String.
        queue.Formatter = new XmlMessageFormatter(new Type[]
            {typeof(String)});

        // Receive the message from the queue.  Because the Id of the message 
        // , it might not be the message just sent.
        msg = queue.Receive(MessageQueueTransactionType.Single); 

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft