Ejemplo n.º 1
0
        private async Task <ConsumeResult> InvokeUsingBinding(object message, MessageContextData messageContextData, IBinding binding)
        {
            using (var context = new MessageContext
            {
                Config = config,
                Queue = queueName,
                Exchange = messageContextData.Exchange,
                RoutingKey = messageContextData.RoutingKey,
                Message = message,
                Properties = messageContextData.Properties,
                Binding = binding
            })
            {
                try
                {
                    await MiddlewareHelper.GoAsync(config.Middleware.Message,
                                                   async (handler, next) => await handler.Handle(context, next),
                                                   async() => { await binding.Invoke(context); });

                    await binding.Cleanup(context, ConsumeResult.Success);

                    return(ConsumeResult.Success);
                }
                catch (Exception invokeException)
                {
                    var exceptionContext = new ExceptionStrategyContext(context, invokeException);
                    HandleException(exceptionContext);

                    await binding.Cleanup(context, exceptionContext.ConsumeResult);

                    return(exceptionContext.ConsumeResult);
                }
            }
        }
Ejemplo n.º 2
0
        private void HandleException(ExceptionStrategyContext exceptionContext)
        {
            if (cancellationToken.IsCancellationRequested && IgnoreExceptionDuringShutdown(exceptionContext.Exception))
            {
                // The service is most likely stopping, and the connection is gone anyways.
                exceptionContext.SetConsumeResult(ConsumeResult.Requeue);
                return;
            }

            try
            {
                exceptionStrategy.HandleException(exceptionContext);
            }
            catch (Exception strategyException)
            {
                // Exception in the exception strategy. Oh dear.
                exceptionContext.SetConsumeResult(ConsumeResult.Error);
                logger.ConsumeException(strategyException, exceptionContext.MessageContext, ConsumeResult.Error);
            }

            logger.ConsumeException(exceptionContext.Exception, exceptionContext.MessageContext, exceptionContext.ConsumeResult);
        }
Ejemplo n.º 3
0
        /// <inheritdoc />
        public async Task <ConsumeResult> Consume(string exchange, string routingKey, IMessageProperties properties, byte[] body)
        {
            object message = null;

            try
            {
                message = messageSerializer.Deserialize(body, properties);
                if (message == null)
                {
                    throw new ArgumentException("Message body could not be deserialized into a message object", nameof(body));
                }

                return(await DispatchMessage(message, new MessageContextData
                {
                    Exchange = exchange,
                    RoutingKey = routingKey,
                    Properties = properties
                }));
            }
            catch (Exception dispatchException)
            {
                using (var emptyContext = new MessageContext
                {
                    Config = config,
                    Queue = queueName,
                    Exchange = exchange,
                    RoutingKey = routingKey,
                    Message = message,
                    Properties = properties,
                    Binding = null
                })
                {
                    var exceptionContext = new ExceptionStrategyContext(emptyContext, dispatchException);
                    HandleException(exceptionContext);
                    return(exceptionContext.ConsumeResult);
                }
            }
        }
Ejemplo n.º 4
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);
                    }
                }
            });
        }