本文整理汇总了C#中IServiceBus.SubscribeContextHandler方法的典型用法代码示例。如果您正苦于以下问题:C# IServiceBus.SubscribeContextHandler方法的具体用法?C# IServiceBus.SubscribeContextHandler怎么用?C# IServiceBus.SubscribeContextHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IServiceBus
的用法示例。
在下文中一共展示了IServiceBus.SubscribeContextHandler方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DoTest
static void DoTest(IServiceBus publishingBus, IServiceBus receivingBus)
{
int numberOfFailures = new Random().Next(5) + 1;
int receiveCount = 0;
Guid testId = Guid.NewGuid();
Trace.WriteLine(string.Format("Number of failures for message: {0}", numberOfFailures));
using (var waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset))
{
receivingBus.SubscribeContextHandler<Message>(context =>
{
try
{
Message message = context.Message;
// ignore messages from older tests
if (message.TestId != testId)
return;
Trace.Write("Processing message...", "Handler");
receiveCount++;
// retry as many times as requested by message
if (context.RetryCount < message.NumberOfFailures)
{
Trace.WriteLine(string.Format("Message will be retried (retry count = {0}).",
context.RetryCount));
context.RetryLater();
}
else
{
Trace.WriteLine("Message processed.");
waitHandle.Set();
}
}
catch (Exception exc)
{
Trace.WriteLine(exc, "Handler failed");
throw;
}
});
publishingBus.Publish(new Message {NumberOfFailures = numberOfFailures, TestId = testId});
Assert.True(waitHandle.WaitOne(Debugger.IsAttached
? TimeSpan.FromHours(1)
: TimeSpan.FromSeconds(10)));
}
Assert.AreEqual(numberOfFailures + 1, receiveCount);
}