private void DisposeCurrentConsumer(ChannelWrapper channelWrapper) { if (channelWrapper?.Consumer != null) { ExecutionLog.Info($"Closing the consumer channel with channelNumber: {UniqueId} and queueName: {_queueName}."); channelWrapper.Consumer.Received -= OnDataReceived; channelWrapper.Consumer.Registered -= OnRegistered; channelWrapper.Consumer.Unregistered -= OnUnregistered; channelWrapper.Consumer.Shutdown -= OnShutdown; channelWrapper.Consumer.ConsumerCancelled -= OnConsumerCancelled; channelWrapper.Consumer = null; } }
/// <summary> /// Returns a <see cref="ChannelWrapper"/> containing a channel used to communicate with the broker /// </summary> /// <returns>a <see cref="ChannelWrapper"/> containing a channel used to communicate with the broker</returns> public ChannelWrapper GetChannel(int id) { lock (_lock) { if (_connection == null) { CreateConnection(); } else { if (!_connectionStatus.IsConnected) { // try to reconnect try { // check if the connection can be made var connection = _connectionFactory.CreateConnection(); if (connection.IsOpen) { connection.Close(); RemoveChannels(); RemoveConnection(); Thread.Sleep(1000); CreateConnection(); } } catch (Exception) { // ignored } } } if (!_connection.IsOpen) { throw new ConnectFailureException("Cannot create the channel because the connection is closed.", null); } if (_models.TryGetValue(id, out var wrapper)) { if (wrapper != null) { return(wrapper); } } var model = _connection.CreateModel(); //ExecutionLog.LogDebug($"Channel created with internal number: {model?.ChannelNumber}."); wrapper = new ChannelWrapper(id, model); if (_models.ContainsKey(id)) { _models.Remove(id); _models.Add(id, wrapper); //_connection.AutoClose = true; return(wrapper); } _models.Add(id, wrapper); //_connection.AutoClose = true; return(wrapper); } }