Skip to content

Instantly share code, notes, and snippets.

@gdyrrahitis

gdyrrahitis/RabbitMqClientBase.cs Secret

Last active May 16, 2020
Embed
What would you like to do?
public abstract class RabbitMqClientBase : IDisposable
{
protected const string VirtualHost = "CUSTOM_HOST";
protected readonly string LoggerExchange = $"{VirtualHost}.LoggerExchange";
protected readonly string LoggerQueue = $"{VirtualHost}.log.message";
protected const string LoggerQueueAndExchangeRoutingKey = "log.message";
protected IModel Channel { get; private set; }
private IConnection _connection;
private readonly ConnectionFactory _connectionFactory;
private readonly ILogger<RabbitMqClientBase> _logger;
protected RabbitMqClientBase(
ConnectionFactory connectionFactory,
ILogger<RabbitMqClientBase> logger)
{
_connectionFactory = connectionFactory;
_logger = logger;
ConnectToRabbitMq();
}
private void ConnectToRabbitMq()
{
if (_connection == null || _connection.IsOpen == false)
{
_connection = _connectionFactory.CreateConnection();
}
if (Channel == null || Channel.IsOpen == false)
{
Channel = _connection.CreateModel();
Channel.ExchangeDeclare(
exchange: LoggerExchange,
type: "direct",
durable: true,
autoDelete: false);
Channel.QueueDeclare(
queue: LoggerQueue,
durable: false,
exclusive: false,
autoDelete: false);
Channel.QueueBind(
queue: LoggerQueue,
exchange: LoggerExchange,
routingKey: LoggerQueueAndExchangeRoutingKey);
}
}
public void Dispose()
{
try
{
Channel?.Close();
Channel?.Dispose();
Channel = null;
_connection?.Close();
_connection?.Dispose();
_connection = null;
}
catch (Exception ex)
{
_logger.LogCritical(ex, "Cannot dispose RabbitMQ channel or connection");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.