MessageQueue.ReceiveById Method (String, TimeSpan, MessageQueueTransactionType)
Receives the message that matches the given identifier and waits until either a message with the specified identifier is available in the queue or the time-out expires.
Assembly: System.Messaging (in System.Messaging.dll)
public Message ReceiveById( string id, TimeSpan timeout, MessageQueueTransactionType transactionType )
- Type: System.TimeSpan
A TimeSpan that indicates the time to wait until a new message is available for inspection.
The id parameter is null.
A message with the specified id did not arrive in the queue before the time-out expired.
An error occurred when accessing a Message Queuing method.
The transactionType parameter is not one of the MessageQueueTransactionType members.
Use this method to read a message with a known identifier and remove it from the queue. This method returns immediately if the message with the identifier specified by the id parameter is in the queue, using a transaction context defined by the transactionType parameter. Otherwise, the method waits the given period of time for a new message to arrive. If a new message does not arrive before the time-out expires, an exception is thrown.
The timeout parameter does not specify the total running time for this method. Rather, it specifies the time to wait for a new message to arrive in the queue. Each time a new message arrives, this method examines the Id of the new message to see if it matches the id parameter. If not, this method starts the time-out period over and waits for another new message to arrive. Therefore, if new messages continue to arrive within the time-out period, it is possible for this method to continue running indefinitely, either until the time-out period expires without any new messages arriving, or until a message arrives whose Id matches the id parameter.
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 Id property of a message is unique across the Message Queuing enterprise, so there will be at most one message in the queue that matches the given id parameter. If the message with the specified identifier is in a queue other than the one associated with this MessageQueue instance, the message will not be found.
Use this overload of ReceiveById(String) when it is acceptable for the current thread to be blocked as long as new messages continue to arrive in the queue within the time-out period specified by the timeout parameter. The thread will be blocked for at least the given period of time, or indefinitely if you specified the value InfiniteTimeout for the timeout parameter, or if new messages continue to arrive in the queue within the time-out period specified by the timeout parameter.
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.
Two other methods allow you to receive messages from a queue. The Receive method returns the first message in the queue, and the ReceiveByCorrelationId(String) method is used to retrieve an acknowledgment, report, or application-generated response message that was created as a result of a message sent to the queue.
To read a message with a specified identifier without removing it from the queue, use the PeekById(String) method. The PeekById(String) 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 PeekById(String). Because PeekById(String) does not remove any messages in the queue, there would be nothing to roll back if the transaction were aborted.
The following table shows whether this method is available in various Workgroup modes.
Local computer and direct format name
Remote computer and direct format name
The following code example demonstrates the use of .
// 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, "Example Message Label", MessageQueueTransactionType.Single); // Get the message's Id property value. string id = msg.Id; // Receive the message from the queue. msg = queue.ReceiveById(id, TimeSpan.FromSeconds(10.0), MessageQueueTransactionType.Single);
- Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.