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
)

Parameters

correlationId
Type: System.String

The CorrelationId of the message to peek.

timeout
Type: System.TimeSpan

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

Return Value

Type: System.Messaging.Message
The Message whose CorrelationId matches the correlationId parameter passed in.

ExceptionCondition
ArgumentNullException

The correlationId parameter is null.

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

Show:
© 2014 Microsoft