MessageQueue.PeekById Method (String)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Peeks the message whose message identifier matches the id parameter.

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

public Message PeekById(
	string id


Type: System.String

The Id of the message to peek.

Return Value

Type: System.Messaging.Message

The Message whose Id property matches the id parameter.

Exception Condition

The id parameter is null.


No message with the specified id exists.


An error occurred when accessing a Message Queuing method.

Use PeekById(String) to read, without removing from the queue, a message that has a known message identifier. The identifier 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. This overload throws an exception if the queue does not currently contain the message.

Two additional methods allow you to peek messages in a queue: Peek and PeekByCorrelationId(String). The Peek method returns the first message in the queue; PeekByCorrelationId(String) returns an acknowledgment, report, or application-generated response message that was created as a result of a message sent to the queue.

The following table shows whether this method is available in various Workgroup modes.

Workgroup mode


Local computer


Local computer and direct format name


Remote computer


Remote computer and direct format name


The following code example demonstrates the use of PeekById(String).

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

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

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

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

// Simulate doing other work so the message has time to arrive.

// Peek at the message.
msg = queue.PeekById(id);

.NET Framework
Available since 1.1
Return to top