Esempio n. 1
0
        protected async Task ProcessMessagesAsync(Message message, CancellationToken token)
        {
            if (message is null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            // Process the message.
            var mf = ConsumerSettings.FormatIf(message, _logger.IsEnabled(LogLevel.Debug));

            _logger.LogDebug("Received message - {0}", mf);

            if (token.IsCancellationRequested)
            {
                // Note: Use the cancellationToken passed as necessary to determine if the subscriptionClient has already been closed.
                // If subscriptionClient has already been closed, you can choose to not call CompleteAsync() or AbandonAsync() etc.
                // to avoid unnecessary exceptions.
                _logger.LogDebug("Abandon message - {0}", mf);
                await Client.AbandonAsync(message.SystemProperties.LockToken).ConfigureAwait(false);

                return;
            }

            var exception = await MessageProcessor.ProcessMessage(message).ConfigureAwait(false);

            if (exception != null)
            {
                if (mf == null)
                {
                    mf = ConsumerSettings.FormatIf(message, true);
                }
                _logger.LogError(exception, "Abandon message (exception occured while processing) - {0}", mf);

                try
                {
                    // Execute the event hook
                    ConsumerSettings.OnMessageFault?.Invoke(MessageBus, ConsumerSettings, null, exception, message);
                    MessageBus.Settings.OnMessageFault?.Invoke(MessageBus, ConsumerSettings, null, exception, message);
                }
                catch (Exception eh)
                {
                    MessageBusBase.HookFailed(_logger, eh, nameof(IConsumerEvents.OnMessageFault));
                }

                var messageProperties = new Dictionary <string, object>
                {
                    // Set the exception message
                    ["SMB.Exception"] = exception.Message
                };
                await Client.AbandonAsync(message.SystemProperties.LockToken, messageProperties).ConfigureAwait(false);

                return;
            }

            // Complete the message so that it is not received again.
            // This can be done only if the subscriptionClient is created in ReceiveMode.PeekLock mode (which is the default).
            _logger.LogDebug("Complete message - {0}", mf);
            await Client.CompleteAsync(message.SystemProperties.LockToken).ConfigureAwait(false);
        }
 // Use this handler to examine the exceptions received on the message pump.
 protected Task ExceptionReceivedHandler(ExceptionReceivedEventArgs exceptionReceivedEventArgs)
 {
     try
     {
         // Execute the event hook
         (ConsumerSettings.OnMessageFault ?? MessageBus.Settings.OnMessageFault)?.Invoke(MessageBus, ConsumerSettings, exceptionReceivedEventArgs, exceptionReceivedEventArgs.Exception);
     }
     catch (Exception eh)
     {
         MessageBusBase.HookFailed(_log, eh, nameof(IConsumerEvents.OnMessageFault));
     }
     return(Task.CompletedTask);
 }