MessageQueue.ReceiveByCorrelationId 方法

定义

接收匹配给定相关标识符的消息。

重载

ReceiveByCorrelationId(String, TimeSpan, MessageQueueTransactionType)

接收与给定的相关标识符匹配的消息,并且一直等到队列中出现具有指定相关标识符的消息或超时过期。

ReceiveByCorrelationId(String, TimeSpan, MessageQueueTransaction)

从事务性队列中接收与给定的相关标识符匹配的消息,并且一直等到队列中出现具有指定相关标识符的消息或者超时过期。

ReceiveByCorrelationId(String, TimeSpan)

从非事务性队列中接收与给定的相关标识符匹配的消息,并且一直等到队列中出现具有指定相关标识符的消息或者超时过期。

ReceiveByCorrelationId(String, MessageQueueTransactionType)

接收匹配给定相关标识符的消息,而且在队列中当前不存在具有指定相关标识符的消息时立即引发异常。

ReceiveByCorrelationId(String, MessageQueueTransaction)

从事务性队列中接收与给定的相关标识符匹配的消息,而且在队列中当前不存在具有指定相关标识符的消息时立即引发异常。

ReceiveByCorrelationId(String)

从非事务性队列中接收与给定的相关标识符匹配的消息,而且在队列中当前不存在具有指定相关标识符的消息时立即引发异常。

ReceiveByCorrelationId(String, TimeSpan, MessageQueueTransactionType)

接收与给定的相关标识符匹配的消息,并且一直等到队列中出现具有指定相关标识符的消息或超时过期。

public:
 System::Messaging::Message ^ ReceiveByCorrelationId(System::String ^ correlationId, TimeSpan timeout, System::Messaging::MessageQueueTransactionType transactionType);
public System.Messaging.Message ReceiveByCorrelationId (string correlationId, TimeSpan timeout, System.Messaging.MessageQueueTransactionType transactionType);
member this.ReceiveByCorrelationId : string * TimeSpan * System.Messaging.MessageQueueTransactionType -> System.Messaging.Message
Public Function ReceiveByCorrelationId (correlationId As String, timeout As TimeSpan, transactionType As MessageQueueTransactionType) As Message

参数

correlationId
String

要接收的消息的 CorrelationId

timeout
TimeSpan

一个 TimeSpan 指示有新消息可用于检查之前等待的时间。

transactionType
MessageQueueTransactionType

MessageQueueTransactionType 值之一,它描述与消息关联的事务上下文的类型。

返回

Message 匹配传入的 correlationId 参数的 CorrelationId

例外

correlationId 参数为 null

未能找到具有指定 correlationId 的消息。

timeout 参数指定的值无效,可能是 timeout 小于 Zero 或大于 InfiniteTimeout

transactionType 参数不是 MessageQueueTransactionType 成员之一。

具有指定 correlationId 的消息不在队列中,而且在超时过期之前没有到达。

- 或 -

访问“消息队列”方法时出错。

示例

以下代码示例演示了 ReceiveByCorrelationId(String, TimeSpan, MessageQueueTransactionType) 的用法。


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

// Create a new message.
Message^ msg = gcnew 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 = 
    gcnew MessageQueue(".\\exampleTransQueue");

// Create a new message in response to the original message.
Message^ responseMsg = gcnew 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);

// 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,
    TimeSpan::FromSeconds(10.0), MessageQueueTransactionType::Single);

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

transQueue->Close();
queue->Close();

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

// 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,
    TimeSpan.FromSeconds(10.0), MessageQueueTransactionType.Single);

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

注解

此方法在 引用MessageQueue的队列中查找与指定correlationId参数匹配的消息CorrelationId。 如果使用 参数定义的transactionType事务上下文,具有参数指定的correlationId相关标识符的消息在队列中,则此方法将立即返回 。 否则, 方法将等待给定的时间段,以便新消息到达。 如果新消息在超时到期之前未到达,则会引发异常。

参数 timeout 不指定此方法的总运行时间。 而是指定等待新消息到达队列的时间。 每当新消息到达时,此方法都会 CorrelationId 检查新消息的 ,以查看它是否与 参数匹配 correlationId 。 否则,此方法将启动超时期限,并等待另一条新消息到达。 因此,如果新消息在超时期限内继续到达,则此方法可能会无限期地继续运行,直到超时期限到期且没有任何新消息到达,或者直到到达与 CorrelationId 参数匹配 correlationId 的消息。 如果已将外部事务上下文附加到要用于接收消息的线程,则为 transactionType 参数指定 Automatic 。 指定 Single 是否要以单个内部事务的形式接收消息。 可以指定 None 是否要从事务上下文之外的事务队列接收消息。

如果调用此方法以接收来自事务队列的消息,则事务中止时收到的消息将返回到队列。 在提交事务之前,不会从队列中永久删除该消息。

属性 CorrelationId 用于将发送到队列的消息绑定到关联的响应、报告或确认消息。

另外两种方法允许从队列接收消息。 方法 Receive 返回队列中的第一条消息,方法 ReceiveById(String) 用于通过指定消息的唯一标识符来检索消息。

若要读取具有指定关联标识符的消息而不将其从队列中删除,请使用 PeekByCorrelationId(String) 方法。 方法 PeekByCorrelationId(String) 始终返回队列中的第一条消息,因此对 方法的后续调用将返回相同的消息,除非队列中到达了更高优先级的消息。 没有与调用 PeekByCorrelationId(String)返回的消息关联的事务上下文。 由于 PeekByCorrelationId(String) 不会删除队列中的任何消息,因此,如果中止事务,则不会回滚任何消息。

下表显示了此方法在各种工作组模式下是否可用。

工作组模式 可用
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于

ReceiveByCorrelationId(String, TimeSpan, MessageQueueTransaction)

从事务性队列中接收与给定的相关标识符匹配的消息,并且一直等到队列中出现具有指定相关标识符的消息或者超时过期。

public:
 System::Messaging::Message ^ ReceiveByCorrelationId(System::String ^ correlationId, TimeSpan timeout, System::Messaging::MessageQueueTransaction ^ transaction);
public System.Messaging.Message ReceiveByCorrelationId (string correlationId, TimeSpan timeout, System.Messaging.MessageQueueTransaction transaction);
member this.ReceiveByCorrelationId : string * TimeSpan * System.Messaging.MessageQueueTransaction -> System.Messaging.Message
Public Function ReceiveByCorrelationId (correlationId As String, timeout As TimeSpan, transaction As MessageQueueTransaction) As Message

参数

correlationId
String

要接收的消息的 CorrelationId

timeout
TimeSpan

一个 TimeSpan 指示有新消息可用于检查之前等待的时间。

返回

Message 匹配传入的 correlationId 参数的 CorrelationId

例外

correlationId 参数为 null

- 或 -

transaction 参数为 null

timeout 参数指定的值无效,可能是 timeout 小于 Zero 或大于 InfiniteTimeout

具有指定 correlationId 的消息不在队列中,而且在超时过期之前没有到达。

- 或 -

该队列为非事务性队列。

- 或 -

访问“消息队列”方法时出错。

示例

以下代码示例演示了 ReceiveByCorrelationId(String, TimeSpan, MessageQueueTransaction) 的用法。


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

// Create a new message.
Message^ msg = gcnew 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 = 
    gcnew MessageQueue(".\\exampleTransQueue");

// Create a new message in response to the original message.
Message^ responseMsg = gcnew 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);

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

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

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

    // Receive the response message from the transactional queue.
    responseMsg = transQueue->ReceiveByCorrelationId(id,
        TimeSpan::FromSeconds(10.0), transaction);

    // Commit the transaction.
    transaction->Commit();
}
catch (Exception^ ex)
{
    // Cancel the transaction.
    transaction->Abort();

    // Propagate the exception.
    throw ex;
}
finally
{
    // Dispose of the transaction object.
    delete transaction;
    transQueue->Close();
    queue->Close();
}

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

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

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

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

    // Receive the response message from the transactional queue.
    responseMsg = transQueue.ReceiveByCorrelationId(id,
        TimeSpan.FromSeconds(10.0), 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();
}

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

注解

此方法在 引用MessageQueue的事务队列中查找与指定correlationId参数匹配的消息CorrelationId。 如果使用 参数定义的transaction内部事务上下文,具有参数指定的correlationId相关标识符的消息在队列中,则此方法将立即返回 。 否则, 方法将等待给定的时间段,以便新消息到达。 如果新消息在超时到期之前未到达,则会引发异常。

参数 timeout 不指定此方法的总运行时间。 而是指定等待新消息到达队列的时间。 每当新消息到达时,此方法都会 CorrelationId 检查新消息的 ,以查看它是否与 参数匹配 correlationId 。 否则,此方法将启动超时期限,并等待另一条新消息到达。 因此,如果新消息在超时期限内继续到达,则此方法可能会无限期地继续运行,直到超时期限到期且没有任何新消息到达,或者直到到达与 CorrelationId 参数匹配 correlationId 的消息。

由于此方法是在事务队列上调用的,因此,如果事务中止,收到的消息将返回到队列。 在提交事务之前,不会从队列中永久删除该消息。

属性 CorrelationId 用于将发送到队列的消息绑定到关联的响应、报告或确认消息。

另外两种方法允许从队列接收消息。 方法 Receive 返回队列中的第一条消息,方法 ReceiveById(String) 用于通过指定消息的唯一标识符来检索消息。

若要读取具有指定关联标识符的消息而不将其从队列中删除,请使用 PeekByCorrelationId(String) 方法。 方法 PeekByCorrelationId(String) 始终返回队列中的第一条消息,因此对 方法的后续调用将返回相同的消息,除非队列中到达了更高优先级的消息。 没有与调用 PeekByCorrelationId(String)返回的消息关联的事务上下文。 由于 PeekByCorrelationId(String) 不会删除队列中的任何消息,因此,如果中止事务,则不会回滚任何消息。

下表显示了此方法在各种工作组模式下是否可用。

工作组模式 可用
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于

ReceiveByCorrelationId(String, TimeSpan)

从非事务性队列中接收与给定的相关标识符匹配的消息,并且一直等到队列中出现具有指定相关标识符的消息或者超时过期。

public:
 System::Messaging::Message ^ ReceiveByCorrelationId(System::String ^ correlationId, TimeSpan timeout);
public System.Messaging.Message ReceiveByCorrelationId (string correlationId, TimeSpan timeout);
member this.ReceiveByCorrelationId : string * TimeSpan -> System.Messaging.Message
Public Function ReceiveByCorrelationId (correlationId As String, timeout As TimeSpan) As Message

参数

correlationId
String

要接收的消息的 CorrelationId

timeout
TimeSpan

一个 TimeSpan 指示有新消息可用于检查之前等待的时间。

返回

Message 匹配传入的 correlationId 参数的 CorrelationId

例外

correlationId 参数为 null

timeout 参数指定的值无效,可能是 timeout 小于 Zero 或大于 InfiniteTimeout

具有指定 correlationId 的消息不在队列中,而且在超时过期之前没有到达。

- 或 -

访问“消息队列”方法时出错。

示例

以下代码示例演示了 ReceiveByCorrelationId(String, TimeSpan) 的用法。


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

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

// Designate a queue to receive the acknowledgement message for this
// message.
msg->AdministrationQueue = 
    gcnew 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 = 
    gcnew 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;

// Receive the acknowledgement message from the admin queue.
Message^ ackMsg = adminQueue->ReceiveByCorrelationId(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);

adminQueue->Close();
queue->Close();

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

// Receive the acknowledgement message from the admin queue.
Message ackMsg = adminQueue.ReceiveByCorrelationId(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);

注解

此方法在 引用MessageQueue的非事务队列中查找与指定correlationId参数匹配的消息CorrelationId。 如果具有 参数指定的 correlationId 相关标识符的消息在队列中,则此方法将立即返回 。 否则, 方法将等待给定的时间段,以便新消息到达。 如果新消息在超时到期之前未到达,则会引发异常。

参数 timeout 不指定此方法的总运行时间。 而是指定等待新消息到达队列的时间。 每当新消息到达时,此方法都会 CorrelationId 检查新消息的 ,以查看它是否与 参数匹配 correlationId 。 否则,此方法将启动超时期限,并等待另一条新消息到达。 因此,如果新消息在超时期限内继续到达,则此方法可能会无限期地继续运行,直到超时期限到期且没有任何新消息到达,或者直到到达与 CorrelationId 参数匹配 correlationId 的消息。

属性 CorrelationId 用于将发送到队列的消息绑定到关联的响应、报告或确认消息。

另外两种方法允许从队列接收消息。 方法 Receive 返回队列中的第一条消息,方法 ReceiveById(String) 用于通过指定消息的唯一标识符来检索消息。

若要读取具有指定关联标识符的消息而不将其从队列中删除,请使用 PeekByCorrelationId(String) 方法。 方法 PeekByCorrelationId(String) 始终返回队列中的第一条消息,因此对 方法的后续调用将返回相同的消息,除非队列中到达了更高优先级的消息。

下表显示了此方法在各种工作组模式下是否可用。

工作组模式 可用
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于

ReceiveByCorrelationId(String, MessageQueueTransactionType)

接收匹配给定相关标识符的消息,而且在队列中当前不存在具有指定相关标识符的消息时立即引发异常。

public:
 System::Messaging::Message ^ ReceiveByCorrelationId(System::String ^ correlationId, System::Messaging::MessageQueueTransactionType transactionType);
public System.Messaging.Message ReceiveByCorrelationId (string correlationId, System.Messaging.MessageQueueTransactionType transactionType);
member this.ReceiveByCorrelationId : string * System.Messaging.MessageQueueTransactionType -> System.Messaging.Message
Public Function ReceiveByCorrelationId (correlationId As String, transactionType As MessageQueueTransactionType) As Message

参数

correlationId
String

要接收的消息的 CorrelationId

transactionType
MessageQueueTransactionType

MessageQueueTransactionType 值之一,它描述与消息关联的事务上下文的类型。

返回

Message 匹配传入的 correlationId 参数的 CorrelationId

例外

correlationId 参数为 null

未能找到具有指定 correlationId 的消息。

transactionType 参数不是 MessageQueueTransactionType 成员之一。

访问“消息队列”方法时出错。

示例

以下代码示例演示了 ReceiveByCorrelationId(String, MessageQueueTransactionType) 的用法。


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

// Create a new message.
Message^ msg = gcnew 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 = 
    gcnew MessageQueue(".\\exampleTransQueue");

// Create a new message in response to the original message.
Message^ responseMsg = gcnew 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);

transQueue->Close();
queue->Close();

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

注解

此方法在 引用MessageQueue的队列中查找与指定correlationId参数匹配的消息CorrelationId。 如果未找到与 参数匹配 correlationID 的消息,则会引发异常。 否则,消息将从队列中删除,并使用 参数定义的 transactionType 事务上下文返回到应用程序。

如果已将外部事务上下文附加到要用于接收消息的线程,则为 transactionType 参数指定 Automatic 。 指定 Single 是否要以单个内部事务的形式接收消息。 可以指定 None 是否要从事务上下文之外的事务队列接收消息。

如果调用此方法以接收来自事务队列的消息,则事务中止时收到的消息将返回到队列。 在提交事务之前,不会从队列中永久删除该消息。

属性 CorrelationId 用于将发送到队列的消息绑定到关联的响应、报告或确认消息。

另外两种方法允许从队列接收消息。 方法 Receive 返回队列中的第一条消息,方法 ReceiveById(String) 用于通过指定消息的唯一标识符来检索消息。

若要读取具有指定关联标识符的消息而不将其从队列中删除,请使用 PeekByCorrelationId(String) 方法。 方法 PeekByCorrelationId(String) 始终返回队列中的第一条消息,因此对 方法的后续调用将返回相同的消息,除非队列中到达了更高优先级的消息。 没有与调用 PeekByCorrelationId(String)返回的消息关联的事务上下文。 由于 PeekByCorrelationId(String) 不会删除队列中的任何消息,因此,如果中止事务,则不会回滚任何消息。

下表显示了此方法在各种工作组模式下是否可用。

工作组模式 可用
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于

ReceiveByCorrelationId(String, MessageQueueTransaction)

从事务性队列中接收与给定的相关标识符匹配的消息,而且在队列中当前不存在具有指定相关标识符的消息时立即引发异常。

public:
 System::Messaging::Message ^ ReceiveByCorrelationId(System::String ^ correlationId, System::Messaging::MessageQueueTransaction ^ transaction);
public System.Messaging.Message ReceiveByCorrelationId (string correlationId, System.Messaging.MessageQueueTransaction transaction);
member this.ReceiveByCorrelationId : string * System.Messaging.MessageQueueTransaction -> System.Messaging.Message
Public Function ReceiveByCorrelationId (correlationId As String, transaction As MessageQueueTransaction) As Message

参数

correlationId
String

要接收的消息的 CorrelationId

返回

Message 匹配传入的 correlationId 参数的 CorrelationId

例外

correlationId 参数为 null

- 或 -

transaction 参数为 null

未能找到具有指定 correlationId 的消息。

该队列为非事务性队列。

- 或 -

访问“消息队列”方法时出错。

示例

以下代码示例演示了 ReceiveByCorrelationId(String, MessageQueueTransaction) 的用法。


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

// Create a new message.
Message^ msg = gcnew 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 = 
    gcnew MessageQueue(".\\exampleTransQueue");

// Create a new message in response to the original message.
Message^ responseMsg = gcnew 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;

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

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

    // Receive the response message from the transactional queue.
    responseMsg = transQueue->ReceiveByCorrelationId(id, transaction);

    // Commit the transaction.
    transaction->Commit();
}
catch (Exception^ ex)
{
    // Cancel the transaction.
    transaction->Abort();

    // Propagate the exception.
    throw ex;
}
finally
{
    // Dispose of the transaction object.
    delete transaction;
    transQueue->Close();
    queue->Close(); 
}

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

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

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

    // Receive the response message from the transactional queue.
    responseMsg = transQueue.ReceiveByCorrelationId(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();
}

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

注解

此方法在 引用MessageQueue的事务队列中查找与指定correlationId参数匹配的消息CorrelationId。 如果未找到与 参数匹配 correlationID 的消息,则会引发异常。 否则,消息将从队列中删除,并使用 参数定义的 transaction 内部事务上下文返回到应用程序。

由于此方法是在事务队列上调用的,因此,如果事务中止,收到的消息将返回到队列。 在提交事务之前,不会从队列中永久删除该消息。

属性 CorrelationId 用于将发送到队列的消息绑定到关联的响应、报告或确认消息。

另外两种方法允许从队列接收消息。 方法 Receive 返回队列中的第一条消息,方法 ReceiveById(String) 用于通过指定消息的唯一标识符来检索消息。

若要读取具有指定关联标识符的消息而不将其从队列中删除,请使用 PeekByCorrelationId(String) 方法。 方法 PeekByCorrelationId(String) 始终返回队列中的第一条消息,因此对 方法的后续调用将返回相同的消息,除非队列中到达了更高优先级的消息。 没有与调用 PeekByCorrelationId(String)返回的消息关联的事务上下文。 由于 PeekByCorrelationId(String) 不会删除队列中的任何消息,因此,如果中止事务,则不会回滚任何消息。

下表显示了此方法在各种工作组模式下是否可用。

工作组模式 可用
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于

ReceiveByCorrelationId(String)

从非事务性队列中接收与给定的相关标识符匹配的消息,而且在队列中当前不存在具有指定相关标识符的消息时立即引发异常。

public:
 System::Messaging::Message ^ ReceiveByCorrelationId(System::String ^ correlationId);
public System.Messaging.Message ReceiveByCorrelationId (string correlationId);
member this.ReceiveByCorrelationId : string -> System.Messaging.Message
Public Function ReceiveByCorrelationId (correlationId As String) As Message

参数

correlationId
String

要接收的消息的 CorrelationId

返回

Message 匹配传入的 correlationId 参数的 CorrelationId

例外

correlationId 参数为 null

未能找到具有指定 correlationId 的消息。

访问“消息队列”方法时出错。

示例

下面的代码示例发送和接收一条消息,该消息包含与队列的订单。 它专门在原始消息到达队列或从队列中检索时请求肯定确认。

#using <system.dll>
#using <system.messaging.dll>

using namespace System;
using namespace System::Messaging;
ref class MyNewQueue
{
public:
   static void CreateQueue( String^ queuePath )
   {
      try
      {
         if (  !MessageQueue::Exists( queuePath ) )
         {
            MessageQueue::Create( queuePath );
         }
         else
         {
            Console::WriteLine(  "{0} already exists.", queuePath );
         }
      }
      catch ( MessageQueueException^ e ) 
      {
         Console::WriteLine( e->Message );
      }

   }

   void SendMessage()
   {
      // Connect to a queue on the local computer.
      MessageQueue^ myQueue = gcnew MessageQueue( ".\\myQueue" );

      // Create a new message.
      Message^ myMessage = gcnew Message( "Original Message" );
      myMessage->AdministrationQueue = gcnew MessageQueue( ".\\myAdministrationQueue" );
      myMessage->AcknowledgeType = (AcknowledgeTypes)(AcknowledgeTypes::PositiveReceive | AcknowledgeTypes::PositiveArrival);

      // Send the Order to the queue.
      myQueue->Send( myMessage );
      return;
   }

   String^ ReceiveMessage()
   {
      // Connect to the a queue on the local computer.
      MessageQueue^ myQueue = gcnew MessageQueue( ".\\myQueue" );
      myQueue->MessageReadPropertyFilter->CorrelationId = true;
      array<Type^>^p = gcnew array<Type^>(1);
      p[ 0 ] = String::typeid;
      myQueue->Formatter = gcnew XmlMessageFormatter( p );
      String^ returnString = nullptr;
      try
      {
         // Receive and format the message. 
         Message^ myMessage = myQueue->Receive();

         // Display message information.
         Console::WriteLine( "____________________________________________" );
         Console::WriteLine( "Original message information--" );
         Console::WriteLine( "Body: {0}", myMessage->Body );
         Console::WriteLine( "Id: {0}", myMessage->Id );
         Console::WriteLine( "____________________________________________" );
         returnString = myMessage->Id;
      }
      catch ( MessageQueueException^ ) 
      {
         // Handle Message Queuing exceptions.
      }
      // Handle invalid serialization format.
      catch ( InvalidOperationException^ e ) 
      {
         Console::WriteLine( e->Message );
      }

      // Catch other exceptions as necessary.
      return returnString;
   }

   void ReceiveAcknowledgment( String^ messageId, String^ queuePath )
   {
      bool found = false;
      MessageQueue^ queue = gcnew MessageQueue( queuePath );
      queue->MessageReadPropertyFilter->CorrelationId = true;
      queue->MessageReadPropertyFilter->Acknowledgment = true;
      try
      {
         while ( queue->PeekByCorrelationId( messageId ) != nullptr )
         {
            Message^ myAcknowledgmentMessage = queue->ReceiveByCorrelationId( messageId );

            // Output acknowledgment message information. The correlation Id is identical
            // to the id of the original message.
            Console::WriteLine( "Acknowledgment Message Information--" );
            Console::WriteLine( "Correlation Id: {0}", myAcknowledgmentMessage->CorrelationId );
            Console::WriteLine( "Id: {0}", myAcknowledgmentMessage->Id );
            Console::WriteLine( "Acknowledgment Type: {0}", myAcknowledgmentMessage->Acknowledgment );
            Console::WriteLine( "____________________________________________" );
            found = true;
         }
      }
      catch ( InvalidOperationException^ e ) 
      {
         // This exception would be thrown if there is no (further) acknowledgment message
         // with the specified correlation Id. Only output a message if there are no messages;
         // not if the loop has found at least one.
         if ( found == false )
         {
            Console::WriteLine( e->Message );
         }

         // Handle other causes of invalid operation exception.
      }

   }

};

int main()
{
   // Create a new instance of the class.
   MyNewQueue^ myNewQueue = gcnew MyNewQueue;

   // Create new queues.
   MyNewQueue::CreateQueue( ".\\myQueue" );
   MyNewQueue::CreateQueue( ".\\myAdministrationQueue" );

   // Send messages to a queue.
   myNewQueue->SendMessage();

   // Receive messages from a queue.
   String^ messageId = myNewQueue->ReceiveMessage();

   // Receive acknowledgment message.
   if ( messageId != nullptr )
   {
      myNewQueue->ReceiveAcknowledgment( messageId, ".\\myAdministrationQueue" );
   }

   return 0;
}
using System;
using System.Messaging;

namespace MyProject
{

    /// <summary>
    /// Provides a container class for the example.
    /// </summary>
    public class MyNewQueue
    {

        //**************************************************
        // Provides an entry point into the application.
        //		
        // This example sends and receives a message from
        // a queue.
        //**************************************************

        public static void Main()
        {
            // Create a new instance of the class.
            MyNewQueue myNewQueue = new MyNewQueue();

            // Create new queues.
            CreateQueue(".\\myQueue");
            CreateQueue(".\\myAdministrationQueue");

            // Send messages to a queue.
            myNewQueue.SendMessage();

            // Receive messages from a queue.
            string messageId = myNewQueue.ReceiveMessage();

            // Receive acknowledgment message.
            if(messageId != null)
            {
                myNewQueue.ReceiveAcknowledgment(messageId, ".\\myAdministrationQueue");
            }

            return;
        }

        //**************************************************
        // Creates a new queue.
        //**************************************************

        public static void CreateQueue(string queuePath)
        {
            try	
            {
                if(!MessageQueue.Exists(queuePath))
                {
                    MessageQueue.Create(queuePath);
                }
                else
                {
                    Console.WriteLine(queuePath + " already exists.");
                }
            }
            catch (MessageQueueException e)
            {
                Console.WriteLine(e.Message);
            }
        }

        //**************************************************
        // Sends a string message to a queue.
        //**************************************************
        
        public void SendMessage()
        {

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

            // Create a new message.
            Message myMessage = new Message("Original Message");

            myMessage.AdministrationQueue = new MessageQueue(".\\myAdministrationQueue");
            myMessage.AcknowledgeType = AcknowledgeTypes.PositiveReceive | AcknowledgeTypes.PositiveArrival;

            // Send the Order to the queue.
            myQueue.Send(myMessage);

            return;
        }

        //**************************************************
        // Receives a message containing an Order.
        //**************************************************
        
        public  string ReceiveMessage()
        {
            // Connect to the a queue on the local computer.
            MessageQueue myQueue = new MessageQueue(".\\myQueue");

            myQueue.MessageReadPropertyFilter.CorrelationId = true;

            // Set the formatter to indicate body contains an Order.
            myQueue.Formatter = new XmlMessageFormatter(new Type[]
                {typeof(string)});

            string returnString = null;
            
            try
            {
                // Receive and format the message.
                Message myMessage =	myQueue.Receive();

                // Display message information.
                Console.WriteLine("____________________________________________");
                Console.WriteLine("Original message information--");
                Console.WriteLine("Body: " +myMessage.Body.ToString());
                Console.WriteLine("Id: " + myMessage.Id.ToString());
                Console.WriteLine("____________________________________________");

                returnString =  myMessage.Id;
            }
            
            catch (MessageQueueException)
            {
                // Handle Message Queuing exceptions.
            }

            // Handle invalid serialization format.
            catch (InvalidOperationException e)
            {
                Console.WriteLine(e.Message);
            }
            
            // Catch other exceptions as necessary.

            return returnString;
        }

        //**************************************************
        // Receives a message containing an Order.
        //**************************************************
        
        public void ReceiveAcknowledgment(string messageId, string queuePath)
        {
            bool found = false;
            MessageQueue queue = new MessageQueue(queuePath);
            queue.MessageReadPropertyFilter.CorrelationId = true;
            queue.MessageReadPropertyFilter.Acknowledgment = true;

            try
            {
                while(queue.PeekByCorrelationId(messageId) != null)
                {
                    Message myAcknowledgmentMessage = queue.ReceiveByCorrelationId(messageId);
            
                    // Output acknowledgment message information. The correlation Id is identical
                    // to the id of the original message.
                    Console.WriteLine("Acknowledgment Message Information--");
                    Console.WriteLine("Correlation Id: " + myAcknowledgmentMessage.CorrelationId.ToString());
                    Console.WriteLine("Id: " + myAcknowledgmentMessage.Id.ToString());
                    Console.WriteLine("Acknowledgment Type: " + myAcknowledgmentMessage.Acknowledgment.ToString());
                    Console.WriteLine("____________________________________________");

                    found = true;
                }
            }
            catch (InvalidOperationException e)
            {
                // This exception would be thrown if there is no (further) acknowledgment message
                // with the specified correlation Id. Only output a message if there are no messages;
                // not if the loop has found at least one.
                if(found == false)
                {	
                    Console.WriteLine(e.Message);
                }

                // Handle other causes of invalid operation exception.
            }
        }
    }
}
Imports System.Messaging


' Provides a container class for the example.

Public Class MyNewQueue
   
   

   ' Provides an entry point into the application.       
   ' This example sends and receives a message from
   ' a queue.
      
   Public Shared Sub Main()
         ' Create a new instance of the class.
         Dim myNewQueue As New MyNewQueue()
         
         ' Create new queues.
         CreateQueue(".\myQueue")
         CreateQueue(".\myAdministrationQueue")
         
         ' Send messages to a queue.
         myNewQueue.SendMessage()
         
         ' Receive messages from a queue.
         Dim messageId As String = myNewQueue.ReceiveMessage()
         
         ' Receive acknowledgment message.
         If Not (messageId Is Nothing) Then
            myNewQueue.ReceiveAcknowledgment(messageId, ".\myAdministrationQueue")
         End If
         
         Return
   End Sub
      
      
      ' Creates a new queue.

   Public Shared Sub CreateQueue(queuePath As String)
         Try
            If Not MessageQueue.Exists(queuePath) Then
               MessageQueue.Create(queuePath)
            Else
               Console.WriteLine((queuePath + " already exists."))
            End If
         Catch e As MessageQueueException
            Console.WriteLine(e.Message)
         End Try
   End Sub
       
      
      
 
      ' Sends a string message to a queue.
 
   Public Sub SendMessage()
         
         ' Connect to a queue on the local computer.
         Dim myQueue As New MessageQueue(".\myQueue")
         
         ' Create a new message.
         Dim myMessage As New Message("Original Message")
         
         myMessage.AdministrationQueue = New MessageQueue(".\myAdministrationQueue")
         myMessage.AcknowledgeType = AcknowledgeTypes.PositiveReceive Or AcknowledgeTypes.PositiveArrival
         
         ' Send the Order to the queue.
         myQueue.Send(myMessage)
         
         Return
   End Sub
      
      
      
 
      ' Receives a message containing an Order.
 
   Public Function ReceiveMessage() As String
         ' Connect to the a queue on the local computer.
         Dim myQueue As New MessageQueue(".\myQueue")
         
         myQueue.MessageReadPropertyFilter.CorrelationId = True
         
         
         ' Set the formatter to indicate body contains an Order.
         myQueue.Formatter = New XmlMessageFormatter(New Type() {GetType(String)})
         
         Dim returnString As String = Nothing
         
         Try
            ' Receive and format the message. 
            Dim myMessage As Message = myQueue.Receive()
            
            
            ' Display message information.
            Console.WriteLine("____________________________________________")
            Console.WriteLine("Original message information--")
            Console.WriteLine(("Body: " + myMessage.Body.ToString()))
            Console.WriteLine(("Id: " + myMessage.Id.ToString()))
            Console.WriteLine("____________________________________________")
            
            returnString = myMessage.Id
         
         
                  
         ' Handle invalid serialization format.
         Catch e As InvalidOperationException
            Console.WriteLine(e.Message)
         End Try
         
         ' Catch other exceptions as necessary.
         Return returnString
   End Function 'ReceiveMessage
      
      
 
      ' Receives a message containing an Order.
 
   Public Sub ReceiveAcknowledgment(messageId As String, queuePath As String)
         Dim found As Boolean = False
         Dim queue As New MessageQueue(queuePath)
         queue.MessageReadPropertyFilter.CorrelationId = True
         queue.MessageReadPropertyFilter.Acknowledgment = True
         
         Try
            While Not (queue.PeekByCorrelationId(messageId) Is Nothing)
               Dim myAcknowledgmentMessage As Message = queue.ReceiveByCorrelationId(messageId)
               
               ' Output acknowledgment message information. The correlation Id is identical
               ' to the id of the original message.
               Console.WriteLine("Acknowledgment Message Information--")
               Console.WriteLine(("Correlation Id: " + myAcknowledgmentMessage.CorrelationId.ToString()))
               Console.WriteLine(("Id: " + myAcknowledgmentMessage.Id.ToString()))
               Console.WriteLine(("Acknowledgment Type: " + myAcknowledgmentMessage.Acknowledgment.ToString()))
               Console.WriteLine("____________________________________________")
               
               found = True
            End While
         Catch e As InvalidOperationException
            ' This exception would be thrown if there is no (further) acknowledgment message
            ' with the specified correlation Id. Only output a message if there are no messages;
            ' not if the loop has found at least one.
            If found = False Then
               Console.WriteLine(e.Message)
            End If
         End Try 
   End Sub
 End Class

注解

此方法在 由 MessageQueue 引用的非事务队列中查找与指定correlationId参数匹配的消息CorrelationId。 如果未找到与 参数匹配 correlationID 的消息,则会引发异常。 否则,将从队列中删除消息并返回到应用程序。

属性 CorrelationId 用于将发送到队列的消息绑定到关联的响应、报告或确认消息。

另外两种方法可用于从队列接收消息。 方法 Receive 返回队列中的第一条消息,该方法 ReceiveById(String) 通过指定消息的唯一标识符来检索消息。

若要读取具有指定相关标识符的消息而不将其从队列中删除,请使用 PeekByCorrelationId(String) 方法。 方法 PeekByCorrelationId(String) 始终返回队列中的第一条消息,因此对 方法的后续调用将返回相同的消息,除非优先级较高的消息到达队列。

下表显示了此方法是否在各种工作组模式下可用。

工作组模式 可用
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于