コード例 #1
0
        public async Task Send(ConsumeContext <RoutingSlip> context, IPipe <ConsumeContext <RoutingSlip> > next)
        {
            var timer = Stopwatch.StartNew();

            try
            {
                ExecuteContext <TArguments> executeContext = new HostExecuteContext <TArguments>(HostMetadataCache.Host, _compensateAddress, context);

                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Host: {0} Activity: {1} Executing: {2}", context.ReceiveContext.InputAddress, TypeMetadataCache <TActivity> .ShortName,
                                     executeContext.TrackingNumber);
                }

                await Task.Yield();

                await _activityFactory.Execute(executeContext, _executePipe).ConfigureAwait(false);

                await context.NotifyConsumed(timer.Elapsed, TypeMetadataCache <TActivity> .ShortName).ConfigureAwait(false);

                await next.Send(context).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                await context.NotifyFaulted(timer.Elapsed, TypeMetadataCache <TActivity> .ShortName, ex).ConfigureAwait(false);

                throw;
            }
        }
コード例 #2
0
        public async Task Send(ConsumeContext <RoutingSlip> context, IPipe <ConsumeContext <RoutingSlip> > next)
        {
            var activity = LogContext.IfEnabled(OperationName.Courier.Execute)?.StartExecuteActivity <TActivity, TArguments>(context);

            var timer = Stopwatch.StartNew();

            try
            {
                ExecuteContext <TArguments> executeContext = new HostExecuteContext <TArguments>(_compensateAddress, context);

                LogContext.Debug?.Log("Execute Activity: {TrackingNumber} ({Activity}, {Host})", executeContext.TrackingNumber,
                                      TypeMetadataCache <TActivity> .ShortName, context.ReceiveContext.InputAddress);

                try
                {
                    await Task.Yield();

                    await _executePipe.Send(executeContext).ConfigureAwait(false);

                    var result = executeContext.Result
                                 ?? executeContext.Faulted(new ActivityExecutionException("The activity execute did not return a result"));

                    await result.Evaluate().ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    await executeContext.Faulted(ex).Evaluate().ConfigureAwait(false);
                }

                await context.NotifyConsumed(timer.Elapsed, TypeMetadataCache <TActivity> .ShortName).ConfigureAwait(false);

                await next.Send(context).ConfigureAwait(false);
            }
            catch (OperationCanceledException exception)
            {
                await context.NotifyFaulted(timer.Elapsed, TypeMetadataCache <TActivity> .ShortName, exception).ConfigureAwait(false);

                if (exception.CancellationToken == context.CancellationToken)
                {
                    throw;
                }

                throw new ConsumerCanceledException($"The operation was cancelled by the activity: {TypeMetadataCache<TActivity>.ShortName}");
            }
            catch (Exception ex)
            {
                await context.NotifyFaulted(timer.Elapsed, TypeMetadataCache <TActivity> .ShortName, ex).ConfigureAwait(false);

                LogContext.Error?.Log(ex, "Activity {Activity} execution faulted: {Exception}", TypeMetadataCache <TActivity> .ShortName);

                throw;
            }
            finally
            {
                activity?.Stop();
            }
        }