Skip to content

Instantly share code, notes, and snippets.


gdyrrahitis/ConsumerBase.cs Secret

Created May 16, 2020
What would you like to do?
public abstract class ConsumerBase : RabbitMqClientBase
private readonly IMediator _mediator;
private readonly ILogger<ConsumerBase> _logger;
protected abstract string QueueName { get; }
public ConsumerBase(
IMediator mediator,
ConnectionFactory connectionFactory,
ILogger<ConsumerBase> consumerLogger,
ILogger<RabbitMqClientBase> logger) :
base(connectionFactory, logger)
_mediator = mediator;
_logger = consumerLogger;
protected virtual async Task OnEventReceived<T>(object sender, BasicDeliverEventArgs @event)
var body = Encoding.UTF8.GetString(@event.Body.ToArray());
var message = JsonConvert.DeserializeObject<T>(body);
await _mediator.Send(message);
catch (Exception ex)
_logger.LogCritical(ex, "Error while retrieving message from queue.");
Channel.BasicAck(@event.DeliveryTag, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment