MessageQueue.ReceiveByCorrelationId Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Empfängt eine Meldung mit der angegebenen Korrelations-ID.
Überlädt
ReceiveByCorrelationId(String, TimeSpan, MessageQueueTransactionType) |
Empfängt die Meldung mit der angegebenen Korrelations-ID und wartet, bis eine Meldung mit der angegebenen Korrelations-ID in der Warteschlange verfügbar ist oder das Timeout abläuft. |
ReceiveByCorrelationId(String, TimeSpan, MessageQueueTransaction) |
Empfängt die Meldung mit der angegebenen Korrelations-ID (aus einer transaktionalen Warteschlange) und wartet, bis eine Meldung mit der angegebenen Korrelations-ID in der Warteschlange verfügbar ist oder das Timeout abläuft. |
ReceiveByCorrelationId(String, TimeSpan) |
Empfängt die Meldung mit der angegebenen Korrelations-ID (aus einer nicht transaktionalen Warteschlange) und wartet, bis eine Meldung mit der angegebenen Korrelations-ID in der Warteschlange verfügbar ist oder das Timeout abläuft. |
ReceiveByCorrelationId(String, MessageQueueTransactionType) |
Empfängt die Meldung mit der angegebenen Korrelations-ID und löst sofort eine Ausnahme aus, wenn sich derzeit keine Meldung mit der angegebenen Korrelations-ID in der Warteschlange befindet. |
ReceiveByCorrelationId(String, MessageQueueTransaction) |
Empfängt die Meldung mit der angegebenen Korrelations-ID (aus einer transaktionalen Warteschlange) und löst sofort eine Ausnahme aus, wenn sich derzeit keine Meldung mit der angegebenen Korrelations-ID in der Warteschlange befindet. |
ReceiveByCorrelationId(String) |
Empfängt die Meldung mit der angegebenen Korrelations-ID (aus einer nicht transaktionalen Warteschlange) und löst sofort eine Ausnahme aus, wenn sich derzeit keine Meldung mit der angegebenen Korrelations-ID in der Warteschlange befindet. |
ReceiveByCorrelationId(String, TimeSpan, MessageQueueTransactionType)
Empfängt die Meldung mit der angegebenen Korrelations-ID und wartet, bis eine Meldung mit der angegebenen Korrelations-ID in der Warteschlange verfügbar ist oder das Timeout abläuft.
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
Parameter
- correlationId
- String
Die CorrelationId der zu empfangenden Meldung.
- timeout
- TimeSpan
Eine TimeSpan, die die Zeitspanne angibt, für die auf den Eingang einer neuen Meldung zur Überprüfung gewartet werden soll.
- transactionType
- MessageQueueTransactionType
Einer der MessageQueueTransactionType-Werte, der den Typ des Transaktionskontexts beschreibt, der der Meldung zugeordnet werden soll.
Gibt zurück
Die Message, deren CorrelationId dem übergebenen correlationId
-Parameter entspricht.
Ausnahmen
Der correlationId
-Parameter ist null
.
Die Meldung mit der angegebenen correlationId
wurde nicht gefunden.
Der für den timeout
-Parameter angegebene Wert ist ungültig. Möglicherweise ist der timeout
kleiner als Zero oder größer als der InfiniteTimeout.
Der transactionType
-Parameter ist keiner der MessageQueueTransactionType-Member.
Eine Meldung mit der angegebenen correlationId
ist nicht in der Warteschlange vorhanden und nicht vor Ablauf des Timeouts eingegangen.
- oder -
Fehler beim Zugriff auf eine Message Queuing-Methode.
Beispiele
Das folgende Codebeispiel veranschaulicht die Verwendung von 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);
Hinweise
Diese Methode sucht in der Warteschlange, auf die von MessageQueue verwiesen wird, nach einer Nachricht, die CorrelationId dem angegebenen correlationId
Parameter entspricht. Diese Methode gibt sofort zurück, wenn sich die Nachricht mit dem durch den correlationId
Parameter angegebenen Korrelationsbezeichner in der Warteschlange befindet, wobei ein durch den transactionType
Parameter definierter Transaktionskontext verwendet wird. Andernfalls wartet die -Methode den angegebenen Zeitraum, bis eine neue Nachricht eingeht. Wenn eine neue Nachricht nicht eingeht, bevor das Timeout abläuft, wird eine Ausnahme ausgelöst.
Der timeout
Parameter gibt nicht die Gesamtlaufzeit für diese Methode an. Es gibt vielmehr die Zeit an, bis eine neue Nachricht in der Warteschlange eintrifft. Jedes Mal, wenn eine neue Nachricht eingeht, untersucht diese Methode die CorrelationId der neuen Nachricht, um festzustellen, ob sie mit dem correlationId
Parameter übereinstimmt. Andernfalls beginnt diese Methode mit dem Timeout und wartet, bis eine weitere neue Nachricht eingeht. Wenn also neue Nachrichten weiterhin innerhalb des Timeoutzeitraums eintreffen, ist es möglich, dass diese Methode unbegrenzt weiter ausgeführt wird, bis der Timeoutzeitraum abläuft, ohne dass neue Nachrichten eintreffen, oder bis eine Nachricht eingeht, deren CorrelationIdcorrelationId
Parameter entspricht. Geben Sie Automatic
für den transactionType
Parameter an, wenn bereits ein externer Transaktionskontext an den Thread angefügt ist, den Sie zum Empfangen der Nachricht verwenden möchten. Geben Sie an Single
, ob Sie die Nachricht als einzelne interne Transaktion empfangen möchten. Sie können angeben None
, ob Sie eine Nachricht von einer Transaktionswarteschlange außerhalb eines Transaktionskontexts empfangen möchten.
Wenn diese Methode aufgerufen wird, um eine Nachricht von einer Transaktionswarteschlange zu empfangen, wird die empfangene Nachricht an die Warteschlange zurückgegeben, wenn die Transaktion abgebrochen wird. Die Nachricht wird erst endgültig aus der Warteschlange entfernt, wenn die Transaktion zu einem Commit verpflichtet wurde.
Die CorrelationId -Eigenschaft wird verwendet, um eine an die Warteschlange gesendete Nachricht mit zugeordneten Antwort-, Berichts- oder Bestätigungsmeldungen zu verknüpfen.
Mit zwei anderen Methoden können Sie Nachrichten aus einer Warteschlange empfangen. Die Receive -Methode gibt die erste Nachricht in der Warteschlange zurück, und die ReceiveById(String) -Methode wird verwendet, um eine Nachricht abzurufen, indem sie ihren eindeutigen Bezeichner angibt.
Verwenden Sie die PeekByCorrelationId(String) -Methode, um eine Nachricht mit einem angegebenen Korrelationsbezeichner zu lesen, ohne sie aus der Warteschlange zu entfernen. Die PeekByCorrelationId(String) -Methode gibt immer die erste Nachricht in der Warteschlange zurück, sodass nachfolgende Aufrufe der -Methode dieselbe Nachricht zurückgeben, es sei denn, eine Nachricht mit höherer Priorität wird in der Warteschlange eintreffen. Einer Nachricht, die von einem Aufruf PeekByCorrelationId(String)von zurückgegeben wird, ist kein Transaktionskontext zugeordnet. Da PeekByCorrelationId(String) keine Nachrichten in der Warteschlange entfernt werden, gäbe es kein Rollback, wenn die Transaktion abgebrochen würde.
Die folgende Tabelle zeigt, ob diese Methode in verschiedenen Arbeitsgruppenmodi verfügbar ist.
Arbeitsgruppenmodus | Verfügbar |
---|---|
Lokalem Computer | Ja |
Name des lokalen Computers und des direkten Formats | Ja |
Remotecomputer | Nein |
Name des Remotecomputers und des direkten Formats | Ja |
Weitere Informationen
Gilt für:
ReceiveByCorrelationId(String, TimeSpan, MessageQueueTransaction)
Empfängt die Meldung mit der angegebenen Korrelations-ID (aus einer transaktionalen Warteschlange) und wartet, bis eine Meldung mit der angegebenen Korrelations-ID in der Warteschlange verfügbar ist oder das Timeout abläuft.
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
Parameter
- correlationId
- String
Die CorrelationId der zu empfangenden Meldung.
- timeout
- TimeSpan
Eine TimeSpan, die die Zeitspanne angibt, für die auf den Eingang einer neuen Meldung zur Überprüfung gewartet werden soll.
- transaction
- MessageQueueTransaction
Das MessageQueueTransaction-Objekt.
Gibt zurück
Die Message, deren CorrelationId dem übergebenen correlationId
-Parameter entspricht.
Ausnahmen
Der correlationId
-Parameter ist null
.
- oder -
Der transaction
-Parameter ist null
.
Der für den timeout
-Parameter angegebene Wert ist ungültig. Möglicherweise ist der timeout
kleiner als Zero oder größer als der InfiniteTimeout.
Eine Meldung mit der angegebenen correlationId
ist nicht in der Warteschlange vorhanden und nicht vor Ablauf des Timeouts eingegangen.
- oder -
Die Warteschlange ist nicht transaktional.
- oder -
Fehler beim Zugriff auf eine Message Queuing-Methode.
Beispiele
Das folgende Codebeispiel veranschaulicht die Verwendung von 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);
Hinweise
Diese Methode sucht in der Transaktionswarteschlange, auf die MessageQueue von referenziert wird, nach einer Nachricht, die CorrelationId dem angegebenen correlationId
Parameter entspricht. Diese Methode gibt sofort zurück, wenn sich die Nachricht mit dem vom correlationId
Parameter angegebenen Korrelationsbezeichner unter Verwendung des internen Transaktionskontexts, der durch den Parameter definiert wird, in der transaction
Warteschlange befindet. Andernfalls wartet die -Methode den angegebenen Zeitraum, bis eine neue Nachricht eingeht. Wenn eine neue Nachricht nicht eingeht, bevor das Timeout abläuft, wird eine Ausnahme ausgelöst.
Der timeout
Parameter gibt nicht die Gesamtlaufzeit für diese Methode an. Es gibt vielmehr die Zeit an, bis eine neue Nachricht in der Warteschlange eintrifft. Jedes Mal, wenn eine neue Nachricht eingeht, untersucht diese Methode die CorrelationId der neuen Nachricht, um festzustellen, ob sie mit dem correlationId
Parameter übereinstimmt. Andernfalls beginnt diese Methode mit dem Timeout und wartet, bis eine weitere neue Nachricht eingeht. Wenn also neue Nachrichten weiterhin innerhalb des Timeoutzeitraums eintreffen, ist es möglich, dass diese Methode unbegrenzt weiter ausgeführt wird, bis der Timeoutzeitraum abläuft, ohne dass neue Nachrichten eintreffen, oder bis eine Nachricht eingeht, deren CorrelationIdcorrelationId
Parameter entspricht.
Da diese Methode in einer Transaktionswarteschlange aufgerufen wird, wird die empfangene Nachricht an die Warteschlange zurückgegeben, wenn die Transaktion abgebrochen wird. Die Nachricht wird erst endgültig aus der Warteschlange entfernt, wenn die Transaktion zu einem Commit verpflichtet wurde.
Die CorrelationId -Eigenschaft wird verwendet, um eine an die Warteschlange gesendete Nachricht mit zugeordneten Antwort-, Berichts- oder Bestätigungsmeldungen zu verknüpfen.
Mit zwei anderen Methoden können Sie Nachrichten aus einer Warteschlange empfangen. Die Receive -Methode gibt die erste Nachricht in der Warteschlange zurück, und die ReceiveById(String) -Methode wird verwendet, um eine Nachricht abzurufen, indem sie ihren eindeutigen Bezeichner angibt.
Verwenden Sie die PeekByCorrelationId(String) -Methode, um eine Nachricht mit einem angegebenen Korrelationsbezeichner zu lesen, ohne sie aus der Warteschlange zu entfernen. Die PeekByCorrelationId(String) -Methode gibt immer die erste Nachricht in der Warteschlange zurück, sodass nachfolgende Aufrufe der -Methode dieselbe Nachricht zurückgeben, es sei denn, eine Nachricht mit höherer Priorität wird in der Warteschlange eintreffen. Einer Nachricht, die von einem Aufruf PeekByCorrelationId(String)von zurückgegeben wird, ist kein Transaktionskontext zugeordnet. Da PeekByCorrelationId(String) keine Nachrichten in der Warteschlange entfernt werden, gäbe es kein Rollback, wenn die Transaktion abgebrochen würde.
Die folgende Tabelle zeigt, ob diese Methode in verschiedenen Arbeitsgruppenmodi verfügbar ist.
Arbeitsgruppenmodus | Verfügbar |
---|---|
Lokalem Computer | Ja |
Name des lokalen Computers und des direkten Formats | Ja |
Remotecomputer | Nein |
Name des Remotecomputers und des direkten Formats | Ja |
Weitere Informationen
Gilt für:
ReceiveByCorrelationId(String, TimeSpan)
Empfängt die Meldung mit der angegebenen Korrelations-ID (aus einer nicht transaktionalen Warteschlange) und wartet, bis eine Meldung mit der angegebenen Korrelations-ID in der Warteschlange verfügbar ist oder das Timeout abläuft.
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
Parameter
- correlationId
- String
Die CorrelationId der zu empfangenden Meldung.
- timeout
- TimeSpan
Eine TimeSpan, die die Zeitspanne angibt, für die auf den Eingang einer neuen Meldung zur Überprüfung gewartet werden soll.
Gibt zurück
Die Message, deren CorrelationId dem übergebenen correlationId
-Parameter entspricht.
Ausnahmen
Der correlationId
-Parameter ist null
.
Der für den timeout
-Parameter angegebene Wert ist ungültig. Möglicherweise ist der timeout
kleiner als Zero oder größer als der InfiniteTimeout.
Eine Meldung mit der angegebenen correlationId
ist nicht in der Warteschlange vorhanden und nicht vor Ablauf des Timeouts eingegangen.
- oder -
Fehler beim Zugriff auf eine Message Queuing-Methode.
Beispiele
Das folgende Codebeispiel veranschaulicht die Verwendung von 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);
Hinweise
Diese Methode sucht in der nicht transaktionalen Warteschlange, auf die MessageQueue von verwiesen wird, nach einer Nachricht, die CorrelationId dem angegebenen correlationId
Parameter entspricht. Diese Methode gibt sofort zurück, wenn sich die Nachricht mit dem durch den Parameter angegebenen Korrelationsbezeichner correlationId
in der Warteschlange befindet. Andernfalls wartet die -Methode den angegebenen Zeitraum, bis eine neue Nachricht eingeht. Wenn eine neue Nachricht nicht eingeht, bevor das Timeout abläuft, wird eine Ausnahme ausgelöst.
Der timeout
Parameter gibt nicht die Gesamtlaufzeit für diese Methode an. Stattdessen wird die Zeit angegeben, in der auf das Eintreffen einer neuen Nachricht in der Warteschlange gewartet wird. Jedes Mal, wenn eine neue Nachricht eingeht, untersucht diese Methode die CorrelationId der neuen Nachricht, um festzustellen, ob sie mit dem correlationId
Parameter übereinstimmt. Wenn dies nicht der Fall ist, beginnt diese Methode mit dem Timeoutzeitraum und wartet darauf, dass eine weitere neue Nachricht eingeht. Wenn also neue Nachrichten weiterhin innerhalb des Timeoutzeitraums eingehen, kann diese Methode unbegrenzt weiter ausgeführt werden, entweder bis der Timeoutzeitraum abläuft, ohne dass neue Nachrichten eingehen, oder bis eine Nachricht eingeht, deren CorrelationIdcorrelationId
Parameter entspricht.
Die CorrelationId -Eigenschaft wird verwendet, um eine an die Warteschlange gesendete Nachricht mit zugeordneten Antwort-, Berichts- oder Bestätigungsmeldungen zu verknüpfen.
Mit zwei anderen Methoden können Sie Nachrichten aus einer Warteschlange empfangen. Die Receive -Methode gibt die erste Nachricht in der Warteschlange zurück, und die ReceiveById(String) -Methode wird verwendet, um eine Nachricht durch Angabe ihres eindeutigen Bezeichners abzurufen.
Verwenden Sie die -Methode, um eine Nachricht mit einem angegebenen Korrelationsbezeichner zu lesen, ohne sie aus der PeekByCorrelationId(String) Warteschlange zu entfernen. Die PeekByCorrelationId(String) -Methode gibt immer die erste Nachricht in der Warteschlange zurück, sodass nachfolgende Aufrufe der -Methode dieselbe Nachricht zurückgeben, es sei denn, eine Nachricht mit höherer Priorität wird in der Warteschlange eingetroffen.
Die folgende Tabelle zeigt, ob diese Methode in verschiedenen Arbeitsgruppenmodi verfügbar ist.
Arbeitsgruppenmodus | Verfügbar |
---|---|
Lokalem Computer | Ja |
Name des lokalen Computers und direktes Format | Ja |
Remotecomputer | Nein |
Name des Remotecomputers und des direkten Formats | Ja |
Weitere Informationen
Gilt für:
ReceiveByCorrelationId(String, MessageQueueTransactionType)
Empfängt die Meldung mit der angegebenen Korrelations-ID und löst sofort eine Ausnahme aus, wenn sich derzeit keine Meldung mit der angegebenen Korrelations-ID in der Warteschlange befindet.
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
Parameter
- correlationId
- String
Die CorrelationId der zu empfangenden Meldung.
- transactionType
- MessageQueueTransactionType
Einer der MessageQueueTransactionType-Werte, der den Typ des Transaktionskontexts beschreibt, der der Meldung zugeordnet werden soll.
Gibt zurück
Die Message, deren CorrelationId dem übergebenen correlationId
-Parameter entspricht.
Ausnahmen
Der correlationId
-Parameter ist null
.
Die Meldung mit der angegebenen correlationId
wurde nicht gefunden.
Der transactionType
-Parameter ist keiner der MessageQueueTransactionType-Member.
Fehler beim Zugriff auf eine Message Queuing-Methode.
Beispiele
Das folgende Codebeispiel veranschaulicht die Verwendung von 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);
Hinweise
Diese Methode sucht in der Warteschlange, auf die MessageQueue von verwiesen wird, nach einer Nachricht, die CorrelationId dem angegebenen correlationId
Parameter entspricht. Wenn keine Meldung gefunden wird, die mit dem correlationID
Parameter übereinstimmt, wird eine Ausnahme ausgelöst. Andernfalls wird die Nachricht aus der Warteschlange entfernt und mithilfe eines durch den Parameter definierten Transaktionskontexts an die transactionType
Anwendung zurückgegeben.
Geben Sie Automatic
für den transactionType
Parameter an, wenn bereits ein externer Transaktionskontext an den Thread angefügt ist, den Sie zum Empfangen der Nachricht verwenden möchten. Geben Sie an Single
, ob Sie die Nachricht als einzelne interne Transaktion empfangen möchten. Sie können angeben None
, ob Sie eine Nachricht von einer Transaktionswarteschlange außerhalb eines Transaktionskontexts empfangen möchten.
Wenn diese Methode aufgerufen wird, um eine Nachricht von einer Transaktionswarteschlange zu empfangen, wird die empfangene Nachricht an die Warteschlange zurückgegeben, wenn die Transaktion abgebrochen wird. Die Nachricht wird erst endgültig aus der Warteschlange entfernt, wenn ein Commit für die Transaktion ausgeführt wird.
Die CorrelationId -Eigenschaft wird verwendet, um eine an die Warteschlange gesendete Nachricht mit zugeordneten Antwort-, Berichts- oder Bestätigungsmeldungen zu verknüpfen.
Mit zwei anderen Methoden können Sie Nachrichten aus einer Warteschlange empfangen. Die Receive -Methode gibt die erste Nachricht in der Warteschlange zurück, und die ReceiveById(String) -Methode wird verwendet, um eine Nachricht durch Angabe ihres eindeutigen Bezeichners abzurufen.
Verwenden Sie die -Methode, um eine Nachricht mit einem angegebenen Korrelationsbezeichner zu lesen, ohne sie aus der PeekByCorrelationId(String) Warteschlange zu entfernen. Die PeekByCorrelationId(String) -Methode gibt immer die erste Nachricht in der Warteschlange zurück, sodass nachfolgende Aufrufe der -Methode dieselbe Nachricht zurückgeben, es sei denn, eine Nachricht mit höherer Priorität wird in der Warteschlange eingetroffen. Es gibt keinen Transaktionskontext, der einer Nachricht zugeordnet ist, die von einem Aufruf PeekByCorrelationId(String)von zurückgegeben wird. Da PeekByCorrelationId(String) keine Nachrichten in der Warteschlange entfernt werden, würde kein Rollback durchgeführt, wenn die Transaktion abgebrochen würde.
Die folgende Tabelle zeigt, ob diese Methode in verschiedenen Arbeitsgruppenmodi verfügbar ist.
Arbeitsgruppenmodus | Verfügbar |
---|---|
Lokalem Computer | Ja |
Name des lokalen Computers und direktes Format | Ja |
Remotecomputer | Nein |
Name des Remotecomputers und des direkten Formats | Ja |
Weitere Informationen
Gilt für:
ReceiveByCorrelationId(String, MessageQueueTransaction)
Empfängt die Meldung mit der angegebenen Korrelations-ID (aus einer transaktionalen Warteschlange) und löst sofort eine Ausnahme aus, wenn sich derzeit keine Meldung mit der angegebenen Korrelations-ID in der Warteschlange befindet.
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
Parameter
- correlationId
- String
Die CorrelationId der zu empfangenden Meldung.
- transaction
- MessageQueueTransaction
Das MessageQueueTransaction-Objekt.
Gibt zurück
Die Message, deren CorrelationId dem übergebenen correlationId
-Parameter entspricht.
Ausnahmen
Der correlationId
-Parameter ist null
.
- oder -
Der transaction
-Parameter ist null
.
Die Meldung mit der angegebenen correlationId
wurde nicht gefunden.
Die Warteschlange ist nicht transaktional.
- oder -
Fehler beim Zugriff auf eine Message Queuing-Methode.
Beispiele
Das folgende Codebeispiel veranschaulicht die Verwendung von 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);
Hinweise
Diese Methode sucht in der Transaktionswarteschlange, auf die MessageQueue von verwiesen wird, nach einer Nachricht, die CorrelationId dem angegebenen correlationId
Parameter entspricht. Wenn keine Meldung gefunden wird, die mit dem correlationID
Parameter übereinstimmt, wird eine Ausnahme ausgelöst. Andernfalls wird die Nachricht aus der Warteschlange entfernt und mithilfe des durch den Parameter definierten internen Transaktionskontexts an die transaction
Anwendung zurückgegeben.
Da diese Methode in einer Transaktionswarteschlange aufgerufen wird, wird die empfangene Nachricht an die Warteschlange zurückgegeben, wenn die Transaktion abgebrochen wird. Die Nachricht wird erst endgültig aus der Warteschlange entfernt, wenn ein Commit für die Transaktion ausgeführt wird.
Die CorrelationId -Eigenschaft wird verwendet, um eine an die Warteschlange gesendete Nachricht mit zugeordneten Antwort-, Berichts- oder Bestätigungsmeldungen zu verknüpfen.
Mit zwei anderen Methoden können Sie Nachrichten aus einer Warteschlange empfangen. Die Receive -Methode gibt die erste Nachricht in der Warteschlange zurück, und die ReceiveById(String) -Methode wird verwendet, um eine Nachricht durch Angabe ihres eindeutigen Bezeichners abzurufen.
Verwenden Sie die -Methode, um eine Nachricht mit einem angegebenen Korrelationsbezeichner zu lesen, ohne sie aus der PeekByCorrelationId(String) Warteschlange zu entfernen. Die PeekByCorrelationId(String) -Methode gibt immer die erste Nachricht in der Warteschlange zurück, sodass nachfolgende Aufrufe der -Methode dieselbe Nachricht zurückgeben, es sei denn, eine Nachricht mit höherer Priorität wird in der Warteschlange eingetroffen. Es gibt keinen Transaktionskontext, der einer Nachricht zugeordnet ist, die von einem Aufruf PeekByCorrelationId(String)von zurückgegeben wird. Da PeekByCorrelationId(String) keine Nachrichten in der Warteschlange entfernt werden, würde kein Rollback durchgeführt, wenn die Transaktion abgebrochen würde.
Die folgende Tabelle zeigt, ob diese Methode in verschiedenen Arbeitsgruppenmodi verfügbar ist.
Arbeitsgruppenmodus | Verfügbar |
---|---|
Lokalem Computer | Ja |
Name des lokalen Computers und direktes Format | Ja |
Remotecomputer | Nein |
Name des Remotecomputers und des direkten Formats | Ja |
Weitere Informationen
Gilt für:
ReceiveByCorrelationId(String)
Empfängt die Meldung mit der angegebenen Korrelations-ID (aus einer nicht transaktionalen Warteschlange) und löst sofort eine Ausnahme aus, wenn sich derzeit keine Meldung mit der angegebenen Korrelations-ID in der Warteschlange befindet.
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
Parameter
- correlationId
- String
Die CorrelationId der zu empfangenden Meldung.
Gibt zurück
Die Message, deren CorrelationId dem übergebenen correlationId
-Parameter entspricht.
Ausnahmen
Der correlationId
-Parameter ist null
.
Die Meldung mit der angegebenen correlationId
wurde nicht gefunden.
Fehler beim Zugriff auf eine Message Queuing-Methode.
Beispiele
Im folgenden Codebeispiel wird eine Nachricht gesendet und empfangen, die eine Bestellung an und aus einer Warteschlange enthält. Es fordert insbesondere eine positive Bestätigung an, wenn die ursprüngliche Nachricht die Warteschlange erreicht oder aus der Warteschlange abgerufen wird.
#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
Hinweise
Diese Methode sucht in der nicht transaktionalen Warteschlange, auf die MessageQueue von verwiesen wird, nach einer Nachricht, die CorrelationId dem angegebenen correlationId
Parameter entspricht. Wenn keine Meldung gefunden wird, die mit dem correlationID
Parameter übereinstimmt, wird eine Ausnahme ausgelöst. Andernfalls wird die Nachricht aus der Warteschlange entfernt und an die Anwendung zurückgegeben.
Die CorrelationId -Eigenschaft wird verwendet, um eine an die Warteschlange gesendete Nachricht mit zugeordneten Antwort-, Berichts- oder Bestätigungsmeldungen zu verknüpfen.
Mit zwei anderen Methoden können Sie Nachrichten aus einer Warteschlange empfangen. Die Receive -Methode gibt die erste Nachricht in der Warteschlange zurück, und die ReceiveById(String) -Methode ruft eine Nachricht ab, indem sie ihren eindeutigen Bezeichner angibt.
Verwenden Sie die -Methode, um eine Nachricht mit einem angegebenen Korrelationsbezeichner zu lesen, ohne sie aus der PeekByCorrelationId(String) Warteschlange zu entfernen. Die PeekByCorrelationId(String) -Methode gibt immer die erste Nachricht in der Warteschlange zurück, sodass nachfolgende Aufrufe der -Methode dieselbe Nachricht zurückgeben, es sei denn, eine Nachricht mit höherer Priorität wird in der Warteschlange eingetroffen.
Die folgende Tabelle zeigt, ob diese Methode in verschiedenen Arbeitsgruppenmodi verfügbar ist.
Arbeitsgruppenmodus | Verfügbar |
---|---|
Lokalem Computer | Ja |
Name des lokalen Computers und direktes Format | Ja |
Remotecomputer | Nein |
Name des Remotecomputers und des direkten Formats | Ja |