示例#1
0
        private async Task <MessageHandlerResult> HandleMessages(
            IEnumerable <IInboundMessage> messages,
            Func <IEnumerable <IInboundMessage>, Task> messagesHandler,
            IErrorPolicy errorPolicy, int attempt)
        {
            try
            {
                _messageLogger.LogProcessing(_logger, messages);

                await messagesHandler(messages);

                return(MessageHandlerResult.Success);
            }
            catch (Exception ex)
            {
                _messageLogger.LogProcessingError(_logger, messages, ex);

                if (errorPolicy == null)
                {
                    throw;
                }

                UpdateFailedAttemptsHeader(messages, attempt);

                if (!errorPolicy.CanHandle(messages, ex))
                {
                    throw;
                }

                var action = errorPolicy.HandleError(messages, ex);

                if (action == ErrorAction.StopConsuming)
                {
                    throw;
                }

                return(MessageHandlerResult.Error(action));
            }
        }
        private MessageHandlerResult HandleMessage <TInboundMessage>(TInboundMessage message, Action <TInboundMessage> messageHandler, IErrorPolicy errorPolicy, int attempt)
            where TInboundMessage : IInboundMessage
        {
            try
            {
                message = (TInboundMessage)DeserializeIfNeeded(message);

                _messageLogger.LogProcessing(_logger, message);

                messageHandler(message);

                return(MessageHandlerResult.Success);
            }
            catch (Exception ex)
            {
                _messageLogger.LogProcessingError(_logger, message, ex);

                if (errorPolicy == null)
                {
                    throw;
                }

                UpdateFailedAttemptsHeader(message, attempt);

                if (!errorPolicy.CanHandle(message, ex))
                {
                    throw;
                }

                var action = errorPolicy.HandleError(message, ex);

                if (action == ErrorAction.StopConsuming)
                {
                    throw;
                }

                return(MessageHandlerResult.Error(action));
            }
        }