Export (0) Print
Expand All

MessageQueue.PeekByCorrelationId Method (String, TimeSpan)

Peeks the message that matches the given correlation identifier and waits until either a message with the specified correlation identifier is available in the queue, or the time-out expires.

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

public Message PeekByCorrelationId (
	string correlationId,
	TimeSpan timeout
)
public Message PeekByCorrelationId (
	String correlationId, 
	TimeSpan timeout
)
public function PeekByCorrelationId (
	correlationId : String, 
	timeout : TimeSpan
) : Message

Parameters

correlationId

The CorrelationId of the message to peek.

timeout

A TimeSpan that indicates the time to wait until a new message is available for inspection.

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).

ArgumentException

The value specified for the timeout parameter is not valid, possibly timeout is less than TimeSpan.Zero or greater than MessageQueue.InfiniteTimeout.

InvalidOperationException

The message with the specified correlationId does not exist in the queue and did not arrive before the time-out expired.

MessageQueueException

A message did not arrive before the time-out expired.

-or-

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, and no new message arrives in the queue within the period specified by the timeout parameter, 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 CorrelationId of the new message to see if it matches the correlationId 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 CorrelationId matches the correlationId parameter.

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 peek messages in a queue. The Peek method returns the first message in the queue, and the PeekById(String) method is used to retrieve a message by specifying its unique identifier.

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 PeekByCorrelationId(String,TimeSpan).


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

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

// Designate a queue to receive the acknowledgement message for this
// message.
msg.AdministrationQueue = new MessageQueue(".\\exampleAdminQueue");

// Set the message to generate an acknowledgement message upon its
// arrival.
msg.AcknowledgeType = AcknowledgeTypes.PositiveArrival;

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

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

// Connect to the admin queue.
MessageQueue adminQueue = new MessageQueue(".\\exampleAdminQueue");

// Set the admin queue's MessageReadPropertyFilter property to ensure
// that the acknowledgement message includes the desired properties.
adminQueue.MessageReadPropertyFilter.Acknowledgment = true;
adminQueue.MessageReadPropertyFilter.CorrelationId = true;

// Peek at the acknowledgement message.
Message ackMsg = adminQueue.PeekByCorrelationId(id,
    TimeSpan.FromSeconds(10.0));

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


// Connect to a queue on the local computer.
MessageQueue queue = new MessageQueue(".\\exampleQueue");
// Create a new message.
Message msg = new Message("Example Message Body");
// Designate a queue to receive the acknowledgement message for this
// message.
msg.set_AdministrationQueue(new MessageQueue(".\\exampleAdminQueue"));
// Set the message to generate an acknowledgement message upon its
// arrival.
msg.set_AcknowledgeType(AcknowledgeTypes.PositiveArrival);
// Send the message.
queue.Send(msg, "Example Message Label");
// Get the message's Id property value.
String id = msg.get_Id();
// Receive the message from the queue.
msg = queue.ReceiveById(id, TimeSpan.FromSeconds(10.0));
// Connect to the admin queue.
MessageQueue adminQueue = new MessageQueue(".\\exampleAdminQueue");
// Set the admin queue's MessageReadPropertyFilter property to ensure
// that the acknowledgement message includes the desired properties.
adminQueue.get_MessageReadPropertyFilter().set_Acknowledgment(true);
adminQueue.get_MessageReadPropertyFilter().set_CorrelationId(true);
// Peek at the acknowledgement message.
Message ackMsg = adminQueue.PeekByCorrelationId(id,
    TimeSpan.FromSeconds(10.0));
// Display the acknowledgement message's property values.
Console.WriteLine("Message.Label: {0}", ackMsg.get_Label());
Console.WriteLine("Message.Acknowledgment: {0}",
    ackMsg.get_Acknowledgment());
Console.WriteLine("Message.CorrelationId: {0}",
    ackMsg.get_CorrelationId());

  • Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see .

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft