Export (0) Print
Expand All

MessageQueue.ReceiveByCorrelationId Method (String, MessageQueueTransactionType)

Receives the message that matches the given correlation identifier and immediately raises an exception if no message with the specified correlation identifier currently exists in the queue.

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

public Message ReceiveByCorrelationId (
	string correlationId,
	MessageQueueTransactionType transactionType
)
public Message ReceiveByCorrelationId (
	String correlationId, 
	MessageQueueTransactionType transactionType
)
public function ReceiveByCorrelationId (
	correlationId : String, 
	transactionType : MessageQueueTransactionType
) : Message
Not applicable.

Parameters

correlationId

The CorrelationId of the message to receive.

transactionType

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

Return Value

The Message whose CorrelationId matches the correlationId parameter passed in.

Exception typeCondition

ArgumentNullException

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

InvalidOperationException

The message with the specified correlationId could not be found.

InvalidEnumArgumentException

The transactionType parameter is not one of the MessageQueueTransactionType members.

MessageQueueException

An error occurred when accessing a Message Queuing method.

This method looks in the queue referenced by the MessageQueue for a message whose CorrelationId matches the specified correlationId parameter. If no message is found that matches the correlationID parameter, an exception is thrown. Otherwise, the message is removed from the queue and returned to the application using a transaction context defined by the transactionType 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.

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.

The CorrelationId property is used to tie a message sent to the queue to associated response, report, or acknowledgment messages.

Two other methods allow you to receive messages from a queue. The Receive method returns the first message in the queue, and the ReceiveById(String) method is used to retrieve a message by specifying its unique identifier.

To read a message with a specified correlation identifier without removing it from the queue, use the PeekByCorrelationId(String) method. The PeekByCorrelationId(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 PeekByCorrelationId(String). Because PeekByCorrelationId(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 ReceiveByCorrelationId(String,MessageQueueTransactionType).


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

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

// Send the message to the nontransactional queue.
queue.Send(msg, "Example Message Label");

// Get the message's Id property value.
string id = msg.Id;

// Receive the message from the nontransactional queue.
msg = queue.ReceiveById(id, TimeSpan.FromSeconds(10.0));

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

// Create a new message in response to the original message.
Message responseMsg = new Message("Example Response Message Body");

// Set the response message's CorrelationId property value to the Id
// property value of the original message.
responseMsg.CorrelationId = id;

// Send the response message to the transactional queue.
transQueue.Send(responseMsg, "Example Response Message Label",
    MessageQueueTransactionType.Single);

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

// Set the transactional queue's MessageReadPropertyFilter property to
// ensure that the response message includes the desired properties.
transQueue.MessageReadPropertyFilter.CorrelationId = true;

// Receive the response message from the transactional queue.
responseMsg = transQueue.ReceiveByCorrelationId(id,
    MessageQueueTransactionType.Single);

// Display the response message's property values.
Console.WriteLine("Message.Label: {0}", responseMsg.Label);
Console.WriteLine("Message.CorrelationId: {0}",
    responseMsg.CorrelationId);


// Connect to a nontransactional queue on the local computer.
MessageQueue queue = new MessageQueue(".\\exampleQueue");
// Create a new message.
Message msg = new Message("Example Message Body");
// Send the message to the nontransactional queue.
queue.Send(msg, "Example Message Label");
// Get the message's Id property value.
String id = msg.get_Id();
// Receive the message from the nontransactional queue.
msg = queue.ReceiveById(id, TimeSpan.FromSeconds(10.0));
// Connect to a transactional queue on the local computer.
MessageQueue transQueue = new MessageQueue(".\\exampleTransQueue");
// Create a new message in response to the original message.
Message responseMsg = new Message("Example Response Message Body");
// Set the response message's CorrelationId property value to the Id
// property value of the original message.
responseMsg.set_CorrelationId(id);
// Send the response message to the transactional queue.
transQueue.Send(responseMsg, "Example Response Message Label",
    MessageQueueTransactionType.Single);
// Simulate doing other work so the message has time to arrive.
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10.0));
// Set the transactional queue's MessageReadPropertyFilter property to
// ensure that the response message includes the desired properties.
transQueue.get_MessageReadPropertyFilter().set_CorrelationId(true);
// Receive the response message from the transactional queue.
responseMsg = transQueue.ReceiveByCorrelationId(id,
    MessageQueueTransactionType.Single);
// Display the response message's property values.
Console.WriteLine("Message.Label: {0}", responseMsg.get_Label());
Console.WriteLine("Message.CorrelationId: {0}",
    responseMsg.get_CorrelationId());

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