本文整理汇总了C#中System.Messaging.MessageQueue.ReceiveByCorrelationId方法的典型用法代码示例。如果您正苦于以下问题:C# MessageQueue.ReceiveByCorrelationId方法的具体用法?C# MessageQueue.ReceiveByCorrelationId怎么用?C# MessageQueue.ReceiveByCorrelationId使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Messaging.MessageQueue
的用法示例。
在下文中一共展示了MessageQueue.ReceiveByCorrelationId方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MessageQueue
// 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);
示例2: MessageQueue
// 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);
示例3: MessageQueue
// 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);
示例4: MessageQueue
// 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);
示例5: MessageQueue
// 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);
示例6: Main
//引入命名空间
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.
}
}
}
}