Пример #1
0
        protected async Task ProcessMessagesAsync(Message message, CancellationToken token)
        {
            // Process the message.
            var mf = ConsumerSettings.FormatIf(message, _log.IsDebugEnabled);

            _log.DebugFormat(CultureInfo.InvariantCulture, "Received message - {0}", mf);

            await MessageProcessor.ProcessMessage(message).ConfigureAwait(false);

            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.
                _log.DebugFormat(CultureInfo.InvariantCulture, "Abandon message - {0}", mf);
                await Client.AbandonAsync(message.SystemProperties.LockToken).ConfigureAwait(false);
            }
            else
            {
                // 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).
                _log.DebugFormat(CultureInfo.InvariantCulture, "Complete message - {0}", mf);
                await Client.CompleteAsync(message.SystemProperties.LockToken).ConfigureAwait(false);
            }
        }
Пример #2
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);
        }
Пример #3
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, _log.IsDebugEnabled);

            _log.DebugFormat(CultureInfo.InvariantCulture, "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.
                _log.DebugFormat(CultureInfo.InvariantCulture, "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);
                }
                _log.ErrorFormat(CultureInfo.InvariantCulture, "Abandon message (exception occured while processing) - {0}", exception, mf);

                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).
            _log.DebugFormat(CultureInfo.InvariantCulture, "Complete message - {0}", mf);
            await Client.CompleteAsync(message.SystemProperties.LockToken).ConfigureAwait(false);
        }
Пример #4
0
 public async Task AbandonAsync(string lockToken, IDictionary <string, object> propertiesToModify = null)
 {
     await Client.AbandonAsync(lockToken, propertiesToModify);
 }