From f243d6d58243cdb782f02e0c4e40c81466ffff48 Mon Sep 17 00:00:00 2001 From: Baris Yerlikaya Date: Thu, 27 Jun 2024 16:09:26 +0300 Subject: [PATCH] the connection for consumer is defined here. --- src/Basic.RabbitMQ/MessageConsumer.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Basic.RabbitMQ/MessageConsumer.cs b/src/Basic.RabbitMQ/MessageConsumer.cs index 9265180..65e2293 100644 --- a/src/Basic.RabbitMQ/MessageConsumer.cs +++ b/src/Basic.RabbitMQ/MessageConsumer.cs @@ -1,19 +1,22 @@ namespace Basic.RabbitMQ; -public class MessageConsumer(RabbitMqClientService rabbitMqClientService) : IMessageConsumer +public class MessageConsumer( + RabbitMqClientService rabbitMqClientService, + ConnectionFactory connectionFactory) : IMessageConsumer, IDisposable { + private IConnection _connection; + public IModel Channel(string queueName, string routingKey, ushort prefetchCount = 1) { - var channel = rabbitMqClientService.Connect(queueName); + _connection ??= connectionFactory.CreateConnection(); + var channel = rabbitMqClientService.Connect(_connection, queueName); channel.QueueBind( queue: queueName, exchange: rabbitMqClientService.BrokerOptions.ExchangeName, routingKey: routingKey, arguments: null); - channel.BasicQos(0, prefetchCount, false); - return channel; } @@ -28,4 +31,6 @@ public AsyncEventingBasicConsumer GetConsumer(IModel channel) return consumer; } + + public void Dispose() => _connection?.Dispose(); } \ No newline at end of file