public async Task Send(ConsumeContext <RoutingSlip> context, IPipe <ConsumeContext <RoutingSlip> > next)
        {
            var activity = LogContext.IfEnabled(OperationName.Courier.Compensate)?.StartCompensateActivity <TActivity, TLog>(context);

            var timer = Stopwatch.StartNew();

            try
            {
                CompensateContext <TLog> compensateContext = new HostCompensateContext <TLog>(context);

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

                try
                {
                    await Task.Yield();

                    await _compensatePipe.Send(compensateContext).ConfigureAwait(false);

                    var result = compensateContext.Result
                                 ?? compensateContext.Failed(new ActivityCompensationException("The activity compensation did not return a result"));

                    await result.Evaluate().ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    await compensateContext.Failed(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} compensation faulted", TypeMetadataCache <TActivity> .ShortName);

                throw;
            }
            finally
            {
                activity?.Stop();
            }
        }
示例#2
0
        public async Task Send(ConsumeContext <RoutingSlip> context, IPipe <ConsumeContext <RoutingSlip> > next)
        {
            var timer = Stopwatch.StartNew();

            try
            {
                CompensateContext <TLog> compensateContext = new HostCompensateContext <TLog>(HostMetadataCache.Host, context);

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

                await Task.Yield();

                try
                {
                    var result = await _activityFactory.Compensate(compensateContext, _compensatePipe).Result().ConfigureAwait(false);

                    await result.Evaluate().ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    var result = compensateContext.Failed(ex);

                    await result.Evaluate().ConfigureAwait(false);
                }

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

                await next.Send(context).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _log.Error($"The activity {TypeMetadataCache<TActivity>.ShortName} threw an exception", ex);

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

                throw;
            }
        }