本文整理汇总了C#中System.Messaging.MessageQueue.ReceiveByCorrelationId方法的典型用法代码示例。如果您正苦于以下问题:C# MessageQueue.ReceiveByCorrelationId方法的具体用法?C# MessageQueue.ReceiveByCorrelationId怎么用?C# MessageQueue.ReceiveByCorrelationId使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Messaging.MessageQueue
的用法示例。
在下文中一共展示了MessageQueue.ReceiveByCorrelationId方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReceiveAcknowledgment
//get acknowledgement if message successfully received from the queue.
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.
Console.WriteLine("Acknowledgment Type: " + myAcknowledgmentMessage.Acknowledgment.ToString());
found = true;
}
}
catch (InvalidOperationException e)
{
if (found == false)
{
Console.WriteLine(e.Message);
}
}
catch (NullReferenceException ex)
{
Console.WriteLine(ex.Message);
}
}
示例2: Main
static void Main(string[] args)
{
// MessageQueue q = new MessageQueue(@".\private$\request");
// Message msg = new Message();
// RoutedMessageContract m = new RoutedMessageContract()
//{
// DestinationHub = "mainline",
// DestinationSpoke = "test2"
//};
// msg.Body = m;
// msg.Label = "ServiceStart";
// using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
// {
// q.Send(msg, MessageQueueTransactionType.Automatic);
// scope.Complete();
// }
// Console.WriteLine(msg.Id);
// MessageQueue responder = new MessageQueue(@".\private$\response");
// MessageQueueTransaction tr = new MessageQueueTransaction();
// Message response = responder.ReceiveByCorrelationId(msg.Id, new TimeSpan(0, 0, 20), tr);
// Console.WriteLine(response.Id);
// Console.Read();
for (int i = 0; i < 25; i++)
{
RoutedMessageContract ct = new RoutedMessageContract();
XmlSerializer serial = new XmlSerializer(typeof(List<ActionParameter>));
List<ActionParameter> parameters = new List<ActionParameter>();
ct.DestinationHub = "mainline";
ct.DestinationSpoke = "subSpoke";
ct.StatusCode = Tetsuo.Common.TransmissionStatusCodes.SVC_STOP;
using (StringWriter s = new StringWriter())
{
serial.Serialize(s, parameters);
ct.MessageBody = s.ToString();
}
MessageQueue q = new MessageQueue(@".\private$\request");
Message msg = new Message(ct);
//Thread.Sleep(6005);
ct.StatusCode = Tetsuo.Common.TransmissionStatusCodes.BEGIN;
q = new MessageQueue(@".\private$\mainline.test.request");
msg = new Message(ct);
msg.Label = "DoSomething";
ct.Action = "DoSomething";
ct.IsFromGateway = true;
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
{
q.Send(msg, MessageQueueTransactionType.Automatic);
scope.Complete();
}
ct.StatusCode = Tetsuo.Common.TransmissionStatusCodes.BEGIN;
parameters.Add(new ActionParameter() { Name = "x", ParamType = "System.Int32", Value = new Random().Next(1, 5000) });
parameters.Add(new ActionParameter() { Name = "y", ParamType = "System.Int32", Value = new Random().Next(100, 1200) });
using (StringWriter s = new StringWriter())
{
serial.Serialize(s, parameters);
ct.MessageBody = s.ToString();
}
q = new MessageQueue(@".\private$\backline.test2.request");
msg = new Message(ct);
msg.Label = "Multiply";
ct.Action = "Multiply";
ct.DestinationHub = "backline";
ct.IsFromGateway = true;
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
{
q.Send(msg, MessageQueueTransactionType.Automatic);
scope.Complete();
}
Console.WriteLine(msg.Id);
MessageQueue responder = new MessageQueue(@".\private$\response");
MessageQueueTransaction tr = new MessageQueueTransaction();
responder.MessageReadPropertyFilter = new MessagePropertyFilter() { CorrelationId = true };
Message response = responder.ReceiveByCorrelationId(msg.Id, new TimeSpan(0, 0, 20), tr);
Console.WriteLine(response.Id);
}
Console.Read();
}
示例3: PerformHouseKeeping
// Missing fragments are marked as rejected in a separate transaction and are sent to sender's DLQ on commit.
private void PerformHouseKeeping(string correlationId)
{
string subQueueName = Utilities.GetSubQueueName(correlationId);
string subQueuePath = "FormatName:DIRECT=OS:" + this.queue.MachineName + @"\" + this.queue.QueueName + ";" + subQueueName;
MessageQueue subQueue = new MessageQueue(subQueuePath);
Message messageToReject = null;
// MarkMessageRejected works only with transaction
MessageQueueTransaction rejectTransaction = new MessageQueueTransaction();
rejectTransaction.Begin();
// ReceiveById to clear the first header fragment whose Id is put in all other fragment's correlation id
// This is in a separate try-catch so that it proceeds with marking other fragments in case first fragment
// is the one that is lost
try
{
messageToReject = subQueue.ReceiveById(correlationId, rejectTransaction);
NativeMethods.MarkMessageRejected(subQueue.FormatName, messageToReject.LookupId);
}
catch (MessageQueueException)
{
// Don't do anything
}
catch (InvalidOperationException)
{
// Don't do anything
}
// Marks other fragments in subqueue as rejected
try
{
while (true)
{
messageToReject = subQueue.ReceiveByCorrelationId(correlationId, rejectTransaction);
NativeMethods.MarkMessageRejected(subQueue.FormatName, messageToReject.LookupId);
}
}
catch (MessageQueueException)
{
// Don't do anything and just come out of the loop
}
catch (InvalidOperationException)
{
// Don't do anything and just come out of the loop
}
// Safe reject in case message becomes available in main queue
try
{
messageToReject = this.queue.ReceiveById(correlationId, rejectTransaction);
NativeMethods.MarkMessageRejected(subQueue.FormatName, messageToReject.LookupId);
}
catch (MessageQueueException)
{
// Don't do anything
}
catch (InvalidOperationException)
{
// Don't do anything
}
// Mark other fragments in main queue as rejected
try
{
while (true)
{
messageToReject = this.queue.ReceiveByCorrelationId(correlationId, rejectTransaction);
NativeMethods.MarkMessageRejected(this.queue.FormatName, messageToReject.LookupId);
}
}
catch (MessageQueueException)
{
// Don't do anything and just come out of the loop
}
catch (InvalidOperationException)
{
// Don't do anything and just come out of the loop
}
finally
{
subQueue.Dispose();
subQueue.Close();
rejectTransaction.Commit();
rejectTransaction.Dispose();
}
}