Пример #1
0
        private async Task OnMessageReceivedAsync(
            Func <IQueueMessage, CancellationToken, Task> messageHandler,
            MessageHandlerOptions messageHandlerOptions,
            BasicDeliverEventArgs eventArgs)
        {
            var cancellationToken = CancellationToken.None;

            var message = new RabbitMQQueueMessage(
                eventArgs.Body,
                eventArgs.DeliveryTag,
                eventArgs.BasicProperties);

            try
            {
                await messageHandler(message, cancellationToken);

                if (messageHandlerOptions.AutoComplete)
                {
                    await CompleteAsync(message, cancellationToken);
                }
            }
            catch (Exception ex)
            {
                try
                {
                    var exceptionHandler = messageHandlerOptions?.ExceptionReceivedHandler;

                    if (exceptionHandler != null)
                    {
                        var context = new MessageHandlerExceptionContext(ex);

                        await exceptionHandler(context);
                    }
                }
                catch
                {
                }

                await AbandonAsync(message, cancellationToken);
            }
        }
Пример #2
0
        public override Task <IQueueMessage> DequeueAsync(
            CancellationToken cancellationToken = default(CancellationToken))
        {
            BasicGetResult result;

            lock (_channel)
            {
                result = _channel.BasicGet(_queueName, autoAck: false);
            }

            if (result == null)
            {
                return(Task.FromResult <IQueueMessage>(null));
            }

            var message = new RabbitMQQueueMessage(
                result.Body,
                result.DeliveryTag,
                result.BasicProperties);

            return(Task.FromResult <IQueueMessage>(message));
        }