internal async Task <AmqpIoTSession> EnsureSessionAsync(TimeSpan timeout) { if (_closed) { throw new IotHubException("Device is now offline.", false); } if (Logging.IsEnabled) { Logging.Enter(this, timeout, $"{nameof(EnsureSessionAsync)}"); } bool gain = await _sessionLock.WaitAsync(timeout).ConfigureAwait(false); if (!gain) { throw new TimeoutException(); } try { if (_amqpIoTSession == null || _amqpIoTSession.IsClosing()) { _amqpIoTSession = await _amqpConnectionHolder.OpenSessionAsync(_deviceIdentity, timeout).ConfigureAwait(false); if (Logging.IsEnabled) { Logging.Associate(this, _amqpIoTSession, $"{nameof(_amqpIoTSession)}"); } if (_deviceIdentity.AuthenticationModel == AuthenticationModel.SasIndividual) { _amqpAuthenticationRefresher = await _amqpConnectionHolder.CreateRefresherAsync(_deviceIdentity, timeout).ConfigureAwait(false); if (Logging.IsEnabled) { Logging.Associate(this, _amqpAuthenticationRefresher, $"{nameof(_amqpAuthenticationRefresher)}"); } } _amqpIoTSession.Closed += OnSessionDisconnected; _messageSendingLink = await _amqpIoTSession.OpenTelemetrySenderLinkAsync(_deviceIdentity, timeout).ConfigureAwait(false); _messageSendingLink.Closed += (obj, arg) => { _amqpIoTSession.SafeClose(); }; if (Logging.IsEnabled) { Logging.Associate(this, _messageSendingLink, $"{nameof(_messageSendingLink)}"); } } if (_disposed) { _amqpAuthenticationRefresher?.StopLoop(); _amqpIoTSession.SafeClose(); if (!_deviceIdentity.IsPooling()) { _amqpConnectionHolder.Dispose(); } throw new IotHubException("Device is now offline.", false); } } finally { _sessionLock.Release(); } if (Logging.IsEnabled) { Logging.Exit(this, timeout, $"{nameof(EnsureSessionAsync)}"); } return(_amqpIoTSession); }
internal async Task <AmqpIotSession> EnsureSessionIsOpenAsync(TimeSpan timeout) { if (_closed) { throw new IotHubException("Device is now offline.", false); } Logging.Enter(this, timeout, nameof(EnsureSessionIsOpenAsync)); bool enteredSemaphore = await _sessionSemaphore.WaitAsync(timeout).ConfigureAwait(false); if (!enteredSemaphore) { throw new TimeoutException("Failed to enter the semaphore required for opening an AMQP session."); } try { if (_amqpIotSession == null || _amqpIotSession.IsClosing()) { _amqpIotSession?.SafeClose(); _amqpIotSession = await _amqpConnectionHolder.OpenSessionAsync(_deviceIdentity, timeout).ConfigureAwait(false); Logging.Associate(this, _amqpIotSession, nameof(_amqpIotSession)); if (_deviceIdentity.AuthenticationModel == AuthenticationModel.SasIndividual) { _amqpAuthenticationRefresher = await _amqpConnectionHolder.CreateRefresherAsync(_deviceIdentity, timeout).ConfigureAwait(false); Logging.Associate(this, _amqpAuthenticationRefresher, nameof(_amqpAuthenticationRefresher)); } _amqpIotSession.Closed += OnSessionDisconnected; _messageSendingLink = await _amqpIotSession.OpenTelemetrySenderLinkAsync(_deviceIdentity, timeout).ConfigureAwait(false); _messageSendingLink.Closed += (obj, arg) => { _amqpIotSession.SafeClose(); }; Logging.Associate(this, _messageSendingLink, nameof(_messageSendingLink)); } if (_disposed) { throw new IotHubException("Device is now offline.", false); } } catch (Exception) { Cleanup(); throw; } finally { _sessionSemaphore.Release(); } Logging.Exit(this, timeout, nameof(EnsureSessionIsOpenAsync)); return(_amqpIotSession); }