コード例 #1
0
        public async Task OpenAsync(TimeSpan timeout)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, timeout, $"{nameof(OpenAsync)}");
            }

            try
            {
                Debug.Assert(_amqpIoTSession == null);
                Debug.Assert(IsUsable());

                _amqpIoTSession = await _amqpSessionCreator.CreateSession(
                    _deviceIdentity,
                    timeout).ConfigureAwait(false);

                if (Logging.IsEnabled)
                {
                    Logging.Associate(this, _amqpIoTSession, $"{nameof(_amqpIoTSession)}");
                }
                await _amqpIoTSession.OpenAsync(timeout).ConfigureAwait(false);

                if (_deviceIdentity.AuthenticationModel == AuthenticationModel.SasIndividual)
                {
                    _amqpAuthenticationRefresher = await _amqpAuthenticationRefresherCreator.CreateRefresher(_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 += OnLinkDisconnected;

                if (Logging.IsEnabled)
                {
                    Logging.Associate(this, _messageSendingLink, $"{nameof(_messageSendingLink)}");
                }
            }
            catch (Exception ex) when(!ex.IsFatal())
            {
                if (SetNotUsable() == 0)
                {
                    OnUnitDisconnected?.Invoke(false, EventArgs.Empty);
                }

                throw;
            }
            finally
            {
                if (Logging.IsEnabled)
                {
                    Logging.Exit(this, timeout, $"{nameof(OpenAsync)}");
                }
            }
        }