Export (0) Print
Expand All

MessageQueue.ReceiveById Method (String, MessageQueueTransaction)

Receives the message that matches the given identifier (from a transactional queue) and immediately raises an exception if no message with the specified identifier currently exists in the queue.

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

public Message ReceiveById (
	string id,
	MessageQueueTransaction transaction
)
public Message ReceiveById (
	String id, 
	MessageQueueTransaction transaction
)
public function ReceiveById (
	id : String, 
	transaction : MessageQueueTransaction
) : Message
Not applicable.

Parameters

id

The Id of the message to receive.

transaction

The MessageQueueTransaction object.

Return Value

The Message whose Id property matches the id parameter passed in.

Exception typeCondition

ArgumentNullException

The id parameter is a null reference (Nothing in Visual Basic).

-or-

The transaction parameter is a null reference (Nothing in Visual Basic).

InvalidOperationException

The message with the specified id could not be found.

MessageQueueException

The queue is non-transactional.

-or-

An error occurred when accessing a Message Queuing method.

Use this method to read a message with a known identifier and remove it from the queue, using the internal transaction context defined by the transaction parameter. This method throws an exception immediately if the message is not in the queue

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.

Because this method is called on 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.

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 ReceiveById(String,MessageQueueTransaction).


// 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;

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

// Create a message queuing transaction.
MessageQueueTransaction transaction = new MessageQueueTransaction();

try
{
    // Begin a transaction.
    transaction.Begin();

    // Receive the message from the queue.
    msg = queue.ReceiveById(id, transaction);

    // Commit the transaction.
    transaction.Commit();
}
catch(System.Exception e)
{
    // Cancel the transaction.
    transaction.Abort();

    // Propagate the exception.
    throw e;
}
finally
{
    // Dispose of the transaction object.
    transaction.Dispose();
}


// 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.get_Id();
// Simulate doing other work so the message has time to arrive.
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10.0));
// Create a message queuing transaction.
MessageQueueTransaction transaction = new MessageQueueTransaction();

try {
    // Begin a transaction.
    transaction.Begin();
    // Receive the message from the queue.
    msg = queue.ReceiveById(id, transaction);
    // Commit the transaction.
    transaction.Commit();
}
catch (System.Exception e) {
    // Cancel the transaction.
    transaction.Abort();
    // Propagate the exception.
    throw e;
}
finally {
    // Dispose of the transaction object.
    transaction.Dispose();
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft