diff --git a/src/RawRabbit.IntegrationTests/Features/LostConnectionTests.cs b/src/RawRabbit.IntegrationTests/Features/LostConnectionTests.cs
index cf357270..2223fe47 100644
--- a/src/RawRabbit.IntegrationTests/Features/LostConnectionTests.cs
+++ b/src/RawRabbit.IntegrationTests/Features/LostConnectionTests.cs
@@ -20,7 +20,7 @@ public LostConnectionTests()
public async Task Should_Succeed_To_Send_Response_Even_If_Channel_Is_Closed()
{
/* Setup */
- var channelFactory = new ChannelFactory(new SingleNodeBroker(BrokerConfiguration.Local));
+ var channelFactory = new ChannelFactory(new SingleNodeBroker(BrokerConfiguration.Local), new RawRabbitConfiguration());
var expectedResponse = new FirstResponse {Infered = Guid.NewGuid()};
var reqeuster = BusClientFactory.CreateDefault(TimeSpan.FromHours(1));
var responder = BusClientFactory.CreateDefault(s => s
diff --git a/src/RawRabbit/Common/ChannelFactory.cs b/src/RawRabbit/Common/ChannelFactory.cs
index 6e8081c7..94ce534f 100644
--- a/src/RawRabbit/Common/ChannelFactory.cs
+++ b/src/RawRabbit/Common/ChannelFactory.cs
@@ -3,6 +3,7 @@
using System.Threading;
using RabbitMQ.Client;
using RawRabbit.Configuration;
+using RawRabbit.Logging;
namespace RawRabbit.Common
{
@@ -18,20 +19,21 @@ public interface IChannelFactory : IDisposable
/// in closing and disposing.
///
/// A new instance of an IModel
- IModel CreateChannel();
+ IModel CreateChannel(IConnection connection = null);
}
public class ChannelFactory : IChannelFactory
{
private readonly IConnectionBroker _connectionBroker;
private readonly ConcurrentDictionary> _connectionToChannel;
- private readonly bool _autoDelete;
+ private readonly bool _autoClose;
+ private readonly ILogger _logger = LogManager.GetLogger();
public ChannelFactory(IConnectionBroker connectionBroker, RawRabbitConfiguration config)
{
_connectionBroker = connectionBroker;
_connectionToChannel = new ConcurrentDictionary>();
- _autoDelete = config.AutoDeleteConnection;
+ _autoClose = config.AutoCloseConnection;
}
public void Dispose()
@@ -41,6 +43,7 @@ public void Dispose()
public void CloseAll()
{
+ _logger.LogDebug("Trying to close all connections.");
foreach (var connection in _connectionToChannel.Keys)
{
connection?.Close();
@@ -50,37 +53,41 @@ public void CloseAll()
public IModel GetChannel()
{
var currentConnection = _connectionBroker.GetConnection();
+
if (!_connectionToChannel.ContainsKey(currentConnection))
{
- _connectionToChannel.TryAdd(currentConnection, new ThreadLocal(currentConnection.CreateModel));
+ _connectionToChannel.TryAdd(currentConnection, new ThreadLocal());
+ var newChannel = CreateChannel(currentConnection);
+ _connectionToChannel[currentConnection].Value = newChannel;
}
-
+
var threadChannel = _connectionToChannel[currentConnection];
if (threadChannel.Value.IsOpen)
{
return threadChannel.Value;
}
- var channel = currentConnection.CreateModel();
- if (_autoDelete && !currentConnection.AutoClose)
- {
- currentConnection.AutoClose = true;
- }
+ var channel = CreateChannel(currentConnection);
threadChannel.Value?.Dispose();
threadChannel.Value = channel;
-
+
return threadChannel.Value;
}
- public IModel CreateChannel()
+ public IModel CreateChannel(IConnection connection = null)
{
- var connection = _connectionBroker.GetConnection();
+ connection = connection ?? _connectionBroker.GetConnection();
var channel = connection.CreateModel();
- if (_autoDelete && !connection.AutoClose)
+ if (_autoClose && !connection.AutoClose)
{
+ _logger.LogInformation("Setting AutoClose to true for connection while calling 'CreateChannel'.");
connection.AutoClose = true;
}
+ else
+ {
+ _logger.LogDebug($"AutoClose in settings object is set to: '{_autoClose}' and on connection '{connection.AutoClose}'");
+ }
return channel;
}
}