Esempio n. 1
0
        DeserializeMessage(IQueueMessageContext messageContext, CancellationToken cancellationToken)
        {
            try
            {
                _logger.LogDebug("Attempting to deserialize message with serialization register {Type}",
                                 _serializationRegister.GetType().FullName);
                var messageWithAttributes = _serializationRegister.DeserializeMessage(messageContext.Message.Body);
                return(true, messageWithAttributes.Message, messageWithAttributes.MessageAttributes);
            }
            catch (MessageFormatNotSupportedException ex)
            {
                _logger.LogTrace(ex,
                                 "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.DeleteMessage(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);
            }
        }
        protected override async Task <bool> RunInnerAsync(HandleMessageContext context, Func <CancellationToken, Task <bool> > func, CancellationToken stoppingToken)
        {
            try
            {
                return(await func(stoppingToken).ConfigureAwait(false));
            }
            catch (Exception e)
            {
                _monitor.HandleException(context.MessageType);
                _monitor.HandleError(e, context.RawMessage);

                context.SetException(e);
                return(false);
            }
            finally
            {
                _monitor.Handled(context.Message);
            }
        }
Esempio n. 3
0
        private async Task TryUpdateVisibilityTimeout(HandleMessageContext context, CancellationToken stoppingToken)
        {
            if (!TryGetApproxReceiveCount(context.RawMessage.Attributes, out int approxReceiveCount))
            {
                return;
            }

            TimeSpan backoffDuration = _backoffStrategy.GetBackoffDuration(context.Message, approxReceiveCount, context.HandledException);

            try
            {
                await context.VisibilityUpdater.UpdateMessageVisibilityTimeout(backoffDuration, stoppingToken).ConfigureAwait(false);
            }
            catch (AmazonServiceException ex)
            {
                _logger.LogError(
                    ex,
                    "Failed to update message visibility timeout by {VisibilityTimeout} seconds for message with receipt handle '{ReceiptHandle}'.",
                    backoffDuration,
                    context.RawMessage.ReceiptHandle);

                _monitor.HandleError(ex, context.RawMessage);
            }
        }
        public async Task DispatchMessageAsync(
            IQueueMessageContext messageContext,
            CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            (bool success, Message typedMessage, MessageAttributes attributes) =
                await DeserializeMessage(messageContext, cancellationToken).ConfigureAwait(false);

            if (!success)
            {
                return;
            }

            var       handlingSucceeded = false;
            Exception lastException     = null;

            try
            {
                if (typedMessage != null)
                {
                    _messageContextAccessor.MessageContext =
                        new MessageContext(messageContext.Message, messageContext.QueueUri, attributes);

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

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

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

                _messagingMonitor.HandleError(ex, messageContext.Message);

                lastException = ex;
            }
            finally
            {
                try
                {
                    if (!handlingSucceeded && _messageBackoffStrategy != null)
                    {
                        await UpdateMessageVisibilityTimeout(messageContext,
                                                             typedMessage,
                                                             lastException,
                                                             cancellationToken).ConfigureAwait(false);
                    }
                }
                finally
                {
                    _messageContextAccessor.MessageContext = null;
                }
            }
        }