DeserializeMessage(IQueueMessageContext messageContext, CancellationToken cancellationToken)
        {
            try
            {
                var messageWithAttributes = _serializationRegister.DeserializeMessage(messageContext.Message.Body);
                return(true, messageWithAttributes.Message, messageWithAttributes.MessageAttributes);
            }
            catch (MessageFormatNotSupportedException ex)
            {
                _logger.LogTrace(
                    "Could not handle message with Id '{MessageId}' because a deserializer for the content is not configured. Message body: '{MessageBody}'.",
                    messageContext.Message.MessageId,
                    messageContext.Message.Body);

                await messageContext.DeleteMessageFromQueueAsync(cancellationToken).ConfigureAwait(false);

                _messagingMonitor.HandleError(ex, messageContext.Message);

                return(false, null, null);
            }
#pragma warning disable CA1031
            catch (Exception ex)
#pragma warning restore CA1031
            {
                _logger.LogError(
                    ex,
                    "Error deserializing message with Id '{MessageId}' and body '{MessageBody}'.",
                    messageContext.Message.MessageId,
                    messageContext.Message.Body);

                _messagingMonitor.HandleError(ex, messageContext.Message);

                return(false, null, null);
            }
        }
Exemple #2
0
        private void Given()
        {
            _serializationRegister
            .DeserializeMessage(Arg.Any <string>())
            .Returns(x => throw new TestException("Test from WhenThereAreExceptionsInMessageProcessing"));
            _sqs.ReceiveMessageAsync(
                Arg.Any <ReceiveMessageRequest>(),
                Arg.Any <CancellationToken>())
            .Returns(x => Task.FromResult(GenerateEmptyMessage()));

            _sqs.When(x => x.ReceiveMessageAsync(
                          Arg.Any <ReceiveMessageRequest>(),
                          Arg.Any <CancellationToken>()))
            .Do(x => _callCount++);
        }
Exemple #3
0
        protected virtual void Given()
        {
            LoggerFactory         = new LoggerFactory();
            Sqs                   = Substitute.For <IAmazonSQS>();
            SerializationRegister = Substitute.For <IMessageSerializationRegister>();
            Monitor               = Substitute.For <IMessageMonitor>();
            Handler               = Substitute.For <IHandlerAsync <SimpleMessage> >();
            LoggerFactory         = Substitute.For <ILoggerFactory>();

            var response = GenerateResponseMessage(MessageTypeString, Guid.NewGuid());

            Sqs.ReceiveMessageAsync(
                Arg.Any <ReceiveMessageRequest>(),
                Arg.Any <CancellationToken>())
            .Returns(
                x => Task.FromResult(response),
                x => Task.FromResult(new ReceiveMessageResponse()));

            DeserializedMessage = new SimpleMessage {
                RaisingComponent = "Component"
            };
            SerializationRegister.DeserializeMessage(Arg.Any <string>()).Returns(DeserializedMessage);
        }
        public async Task DispatchMessage(SQSMessage message, CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            Message typedMessage;

            try
            {
                typedMessage = _serializationRegister.DeserializeMessage(message.Body);
            }
            catch (MessageFormatNotSupportedException ex)
            {
                _logger.LogTrace("Could not handle message with Id '{MessageId}' because a deserializer for the content is not configured. Message body: '{MessageBody}'.",
                                 message.MessageId, message.Body);
                await DeleteMessageFromQueue(message.ReceiptHandle).ConfigureAwait(false);

                _onError(ex, message);
                return;
            }
#pragma warning disable CA1031
            catch (Exception ex)
#pragma warning restore CA1031
            {
                _logger.LogError(0, ex, "Error deserializing message with Id '{MessageId}' and body '{MessageBody}'.",
                                 message.MessageId, message.Body);
                _onError(ex, message);
                return;
            }

            var       handlingSucceeded = false;
            Exception lastException     = null;

            try
            {
                if (typedMessage != null)
                {
                    _messageContextAccessor.MessageContext = new MessageContext(message, _queue.Uri);

                    handlingSucceeded = await CallMessageHandler(typedMessage).ConfigureAwait(false);
                }

                if (handlingSucceeded)
                {
                    await DeleteMessageFromQueue(message.ReceiptHandle).ConfigureAwait(false);
                }
            }
#pragma warning disable CA1031
            catch (Exception ex)
#pragma warning restore CA1031
            {
                _logger.LogError(0, ex, "Error handling message with Id '{MessageId}' and body '{MessageBody}'.",
                                 message.MessageId, message.Body);

                if (typedMessage != null)
                {
                    _messagingMonitor.HandleException(typedMessage.GetType());
                }

                _onError(ex, message);

                lastException = ex;
            }
            finally
            {
                if (!handlingSucceeded && _messageBackoffStrategy != null)
                {
                    await UpdateMessageVisibilityTimeout(message, message.ReceiptHandle, typedMessage, lastException).ConfigureAwait(false);
                }

                _messageContextAccessor.MessageContext = null;
            }
        }