示例#1
0
        public override void HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey,
                                                IBasicProperties properties, byte[] body)
        {
            Task.Run(async() =>
            {
                ExceptionDispatchInfo exception = null;
                MessageContext context          = null;
                HandlingResult handlingResult   = null;
                try
                {
                    try
                    {
                        context = new MessageContext
                        {
                            DependencyResolver = dependencyResolver,
                            Queue      = queueName,
                            RoutingKey = routingKey,
                            Properties = properties
                        };

                        await DispatchMesage(context, body);

                        handlingResult = new HandlingResult
                        {
                            ConsumeResponse = ConsumeResponse.Ack,
                            MessageAction   = MessageAction.None
                        };
                    }
                    catch (Exception eDispatch)
                    {
                        exception = ExceptionDispatchInfo.Capture(UnwrapException(eDispatch));
                        logger.HandlerException(eDispatch);
                        try
                        {
                            var exceptionStrategyContext = new ExceptionStrategyContext(context, exception.SourceException);

                            exceptionStrategy.HandleException(exceptionStrategyContext);

                            handlingResult = exceptionStrategyContext.HandlingResult.ToHandlingResult();
                        }
                        catch (Exception eStrategy)
                        {
                            logger.HandlerException(eStrategy);
                        }
                    }
                    try
                    {
                        if (handlingResult == null)
                        {
                            handlingResult = new HandlingResult
                            {
                                ConsumeResponse = ConsumeResponse.Nack,
                                MessageAction   = MessageAction.None
                            };
                        }
                        await RunCleanup(context, handlingResult);
                    }
                    catch (Exception eCleanup)
                    {
                        logger.HandlerException(eCleanup);
                    }
                }
                finally
                {
                    try
                    {
                        if (handlingResult == null)
                        {
                            handlingResult = new HandlingResult
                            {
                                ConsumeResponse = ConsumeResponse.Nack,
                                MessageAction   = MessageAction.None
                            };
                        }
                        await worker.Respond(deliveryTag, handlingResult.ConsumeResponse);
                    }
                    catch (Exception eRespond)
                    {
                        logger.HandlerException(eRespond);
                    }
                    try
                    {
                        if (context != null)
                        {
                            context.Dispose();
                        }
                    }
                    catch (Exception eDispose)
                    {
                        logger.HandlerException(eDispose);
                    }
                }
            });
        }