protected IMessage ToMessagingMessage(IMessage amqpMessage)
        {
            if (BatchingStrategy.CanDebatch(amqpMessage.Headers))
            {
                var list = new List <object>();
                BatchingStrategy.DeBatch(amqpMessage, fragment =>
                {
                    var convertedObject = MessageConverter.FromMessage(amqpMessage, null);
                    if (convertedObject == null)
                    {
                        throw new MessageConversionException("Message converter returned null");
                    }

                    list.Add(convertedObject);
                });

                return(RabbitMessageBuilder.WithPayload(list).CopyHeaders(amqpMessage.Headers).Build());
            }

            PreprocesMessage(amqpMessage);
            var headers         = amqpMessage.Headers;
            var convertedObject = MessageConverter.FromMessage(amqpMessage, InferredArgumentType);

            if (convertedObject == null)
            {
                throw new MessageConversionException("Message converter returned null");
            }

            var builder = (convertedObject is IMessage) ? RabbitMessageBuilder.FromMessage((IMessage)convertedObject) : RabbitMessageBuilder.WithPayload(convertedObject);
            var message = builder.CopyHeadersIfAbsent(headers).Build();

            return(message);
        }
        protected void InvokeHandlerAndProcessResult(IMessage amqpMessage, RC.IModel channel, IMessage message)
        {
            _logger?.LogDebug("Processing [{message}]", message);
            InvocationResult result = null;

            try
            {
                if (Method == null)
                {
                    var accessor = RabbitHeaderAccessor.GetMutableAccessor(message);
                    accessor.TargetMethod = HandlerAdapter.GetMethodFor(message.Payload);
                }

                result = InvokeHandler(amqpMessage, channel, message);
                if (result.ReturnValue != null)
                {
                    HandleResult(result, amqpMessage, channel, message);
                }
                else
                {
                    _logger?.LogTrace("No result object given - no result to handle");
                }
            }
            catch (ListenerExecutionFailedException e)
            {
                if (ErrorHandler != null)
                {
                    try
                    {
                        var messageWithChannel = RabbitMessageBuilder.FromMessage(message).SetHeader(RabbitMessageHeaders.CHANNEL, channel).Build();
                        var errorResult        = ErrorHandler.HandleError(amqpMessage, messageWithChannel, e);
                        if (errorResult != null)
                        {
                            HandleResult(HandlerAdapter.GetInvocationResultFor(errorResult, message.Payload), amqpMessage, channel, message);
                        }
                        else
                        {
                            _logger?.LogTrace("Error handler returned no result");
                        }
                    }
                    catch (Exception ex)
                    {
                        ReturnOrThrow(amqpMessage, channel, message, ex, ex);
                    }
                }
                else
                {
                    ReturnOrThrow(amqpMessage, channel, message, e.InnerException, e);
                }
            }
        }
        public override void OnMessage(IMessage amqpMessage, RC.IModel channel)
        {
            PreprocesMessage(amqpMessage);
            var headers         = amqpMessage.Headers;
            var convertedObject = MessageConverter.FromMessage(amqpMessage, InferredArgumentType);

            if (convertedObject == null)
            {
                throw new MessageConversionException("Message converter returned null");
            }

            var builder = (convertedObject is IMessage message1) ? RabbitMessageBuilder.FromMessage(message1) : RabbitMessageBuilder.WithPayload(convertedObject);
            var message = builder.CopyHeadersIfAbsent(headers).Build();

            InvokeHandlerAndProcessResult(amqpMessage, channel, message);
        }