コード例 #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)}");
                }
            }
        }
コード例 #2
0
        public async Task <AmqpIoTConnection> EnsureConnection(TimeSpan timeout)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, timeout, $"{nameof(EnsureConnection)}");
            }
            AmqpIoTConnection amqpIoTConnection = null;
            IAmqpIoTAuthenticationRefresher amqpAuthenticationRefresher = null;
            AmqpIoTCbsLink amqpIoTCbsLink = null;
            bool           gain           = await _lock.WaitAsync(timeout).ConfigureAwait(false);

            if (!gain)
            {
                throw new TimeoutException();
            }
            try
            {
                if (_amqpIoTConnection == null)
                {
                    if (Logging.IsEnabled)
                    {
                        Logging.Info(this, "Creating new AmqpConnection", $"{nameof(EnsureConnection)}");
                    }
                    // Create AmqpConnection
                    amqpIoTConnection = await _amqpIoTConnector.OpenConnectionAsync(timeout).ConfigureAwait(false);

                    if (_deviceIdentity.AuthenticationModel != AuthenticationModel.X509)
                    {
                        if (_amqpIoTCbsLink == null)
                        {
                            if (Logging.IsEnabled)
                            {
                                Logging.Info(this, "Creating new AmqpCbsLink", $"{nameof(EnsureConnection)}");
                            }
                            amqpIoTCbsLink = amqpIoTConnection.CreateCbsLink(_deviceIdentity, timeout);
                        }
                        else
                        {
                            amqpIoTCbsLink = _amqpIoTCbsLink;
                        }

                        if (_deviceIdentity.AuthenticationModel == AuthenticationModel.SasGrouped)
                        {
                            if (Logging.IsEnabled)
                            {
                                Logging.Info(this, "Creating connection width AmqpAuthenticationRefresher", $"{nameof(EnsureConnection)}");
                            }
                            amqpAuthenticationRefresher = new AmqpAuthenticationRefresher(_deviceIdentity, amqpIoTCbsLink);
                            await amqpAuthenticationRefresher.InitLoopAsync(timeout).ConfigureAwait(false);
                        }
                    }
                    _amqpIoTConnection           = amqpIoTConnection;
                    _amqpIoTCbsLink              = amqpIoTCbsLink;
                    _amqpAuthenticationRefresher = amqpAuthenticationRefresher;
                    _amqpIoTConnection.Closed   += OnConnectionClosed;
                    if (Logging.IsEnabled)
                    {
                        Logging.Associate(this, _amqpIoTConnection, $"{nameof(_amqpIoTConnection)}");
                    }
                    if (Logging.IsEnabled)
                    {
                        Logging.Associate(this, _amqpIoTCbsLink, $"{nameof(_amqpIoTCbsLink)}");
                    }
                }
                else if (_amqpIoTConnection.IsClosing())
                {
                    throw new IotHubCommunicationException("AMQP connection is closing.");
                }
                else
                {
                    amqpIoTConnection = _amqpIoTConnection;
                }
            }
            catch (Exception ex) when(!ex.IsFatal())
            {
                amqpIoTCbsLink?.Close();
                amqpAuthenticationRefresher?.StopLoop();
                amqpIoTConnection?.SafeClose();
                throw;
            }
            finally
            {
                _lock.Release();
            }
            if (Logging.IsEnabled)
            {
                Logging.Exit(this, timeout, $"{nameof(EnsureConnection)}");
            }
            return(amqpIoTConnection);
        }