private void ClearAmqpClient() { // Have a lock to prevent a race condition lock (m_amqpClientLock) { if (m_amqpClient != null) { try { m_amqpClient.Dispose(); m_amqpClient = null; } catch { } } } }
/// <summary> /// Opens a connection. /// </summary> /// <param name="connRequest">Connection request.</param> /// <exception cref="InvalidOperationException">Thrown if object state is invalid.</exception> /// <exception cref="ObjectDisposedException">Thrown if the object has been disposed.</exception> public void Connect(ConnectionRequest connRequest) { ExpectNotDisposed(); // throws ObjectDisposedException // Locking a relatively large amount of code. However, none of the operations // is expected to last long, and a race condition must not occur. lock (m_amqpClientLock) { if (m_amqpClient != null) { throw new InvalidOperationException("Connection already open"); } // Creating the client object. Using a lock to synchronise the variable between threads. m_amqpClient = CreateAmqpClient(connRequest); // Connecting. This is not within a lock, as the connect operation // supposedly can take relatively long or at least cause a deadlock in this class. m_amqpClient.Connect(connRequest); } }