IModel EnsureChannel() { var lchan = _channel; if (lchan != null && lchan.IsOpen) { return(lchan); } if (_factory == null) { _factory = rabbitMqConnection.ConfigureConnectionFactory(); } if (_conn != null && _conn.IsOpen) { DisposeChannel(); _channel = _conn?.CreateModel(); return(_channel); } DisposeConnection(); var lfac = _factory; if (lfac == null) { throw new Exception("RabbitMq Connection failed to generate a connection factory"); } lfac.RequestedHeartbeat = 60; // The exception `RabbitMQ.Client.Exceptions.BrokerUnreachableException` can happen here if sufficient permissions // are NOT available, or if the network is down: _conn = lfac.CreateConnection(); _channel = _conn?.CreateModel(); return(_channel); }