private async Task HandleMessageAsync(Message message, CancellationToken cancellationToken)
        {
            if (message == null)
            {
                Logger.LogWarning("Received message was null, skipping.");
                return;
            }

            if (_isHostShuttingDown)
            {
                Logger.LogWarning("Abandoning message with ID '{MessageId}' as the host is shutting down.", message.MessageId);
                await AbandonMessageAsync(message.SystemProperties.LockToken);

                return;
            }

            try
            {
                if (String.IsNullOrEmpty(message.CorrelationId))
                {
                    Logger.LogInformation("No operation ID was found on the message");
                }

                MessageCorrelationInfo correlationInfo = message.GetCorrelationInfo();
                Logger.LogInformation(
                    "Received message '{MessageId}' (Transaction: {TransactionId}, Operation: {OperationId}, Cycle: {CycleId})",
                    message.MessageId, correlationInfo.TransactionId, correlationInfo.OperationId, correlationInfo.CycleId);

                var messageContext = new AzureServiceBusMessageContext(message.MessageId, message.SystemProperties,
                                                                       message.UserProperties);

                // Deserialize the message
                TMessage typedMessageBody = DeserializeJsonMessageBody(message.Body, messageContext);

                // Process the message
                // Note - We are not checking for exceptions here as the pump wil handle those and call our exception handling after which it abandons it
                await ProcessMessageAsync(typedMessageBody, messageContext, correlationInfo, cancellationToken);

                Logger.LogInformation("Message {MessageId} processed", message.MessageId);
            }
            catch (Exception ex)
            {
                await HandleReceiveExceptionAsync(ex);
            }
        }
Esempio n. 2
0
        private async Task ProcessMessageAsync(ProcessMessageEventArgs args)
        {
            ServiceBusReceivedMessage message = args?.Message;

            if (message is null)
            {
                Logger.LogWarning("Received message on Azure Service Bus message pump '{JobId}' was null, skipping", JobId);
                return;
            }

            if (_isHostShuttingDown)
            {
                Logger.LogWarning("Abandoning message with ID '{MessageId}' as the Azure Service Bus message pump is shutting down", message.MessageId);
                await args.AbandonMessageAsync(message);

                return;
            }

            if (String.IsNullOrEmpty(message.CorrelationId))
            {
                Logger.LogTrace("No operation ID was found on the message '{MessageId}' during processing in the Azure Service Bus message pump '{JobId}'", message.MessageId, JobId);
            }

            AzureServiceBusMessageContext messageContext  = message.GetMessageContext(JobId);
            MessageCorrelationInfo        correlationInfo = message.GetCorrelationInfo(Settings.Options.Correlation?.TransactionIdPropertyName ?? PropertyNames.TransactionId);
            ServiceBusReceiver            receiver        = args.GetServiceBusReceiver();

            using (IServiceScope serviceScope = ServiceProvider.CreateScope())
            {
                var correlationInfoAccessor = serviceScope.ServiceProvider.GetService <ICorrelationInfoAccessor <MessageCorrelationInfo> >();
                if (correlationInfoAccessor is null)
                {
                    Logger.LogTrace("No message correlation configured in Azure Service Bus message pump '{JobId}' while processing message '{MessageId}'", JobId, message.MessageId);
                    await _messageRouter.RouteMessageAsync(receiver, args.Message, messageContext, correlationInfo, args.CancellationToken);
                }
                else
                {
                    correlationInfoAccessor.SetCorrelationInfo(correlationInfo);
                    using (LogContext.Push(new MessageCorrelationInfoEnricher(correlationInfoAccessor)))
                    {
                        await _messageRouter.RouteMessageAsync(receiver, args.Message, messageContext, correlationInfo, args.CancellationToken);
                    }
                }
            }
        }