当前位置: 首页>>代码示例>>C#>>正文


C# IServiceBus.SubscribeContextHandler方法代码示例

本文整理汇总了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);
        }
开发者ID:cstick,项目名称:MassTransit,代码行数:52,代码来源:RetryLater_Specs.cs


注:本文中的IServiceBus.SubscribeContextHandler方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。