public AmqpIoTSession(AmqpConnection amqpConnection)
        {
            AmqpSessionSettings amqpSessionSettings = new AmqpSessionSettings()
            {
                Properties = new Fields()
            };

            AmqpSession = new AmqpSession(amqpConnection, amqpSessionSettings, AmqpIoTLinkFactory.GetInstance());
            amqpConnection.AddSession(AmqpSession, new ushort?());

            AmqpSession.Closed += _amqpSessionClosed;
        }
Exemplo n.º 2
0
        private async Task <AmqpSession> AmqpSessionCreator(DeviceIdentity deviceIdentity, ILinkFactory linkFactory, AmqpSessionSettings amqpSessionSettings, TimeSpan timeout)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, deviceIdentity, timeout, $"{nameof(AmqpSessionCreator)}");
            }
            AmqpConnection amqpConnection = await EnsureConnection(timeout).ConfigureAwait(false);

            AmqpSession amqpSession = new AmqpSession(amqpConnection, amqpSessionSettings, linkFactory);

            amqpConnection.AddSession(amqpSession, new ushort?());
            if (Logging.IsEnabled)
            {
                Logging.Associate(amqpConnection, amqpSession, $"{nameof(AmqpSessionCreator)}");
            }
            if (Logging.IsEnabled)
            {
                Logging.Exit(this, deviceIdentity, timeout, $"{nameof(AmqpSessionCreator)}");
            }
            return(amqpSession);
        }
        internal async Task <AmqpIotSession> OpenSessionAsync(TimeSpan timeout)
        {
            if (_amqpConnection.IsClosing())
            {
                throw new IotHubCommunicationException("Amqp connection is disconnected.");
            }

            var amqpSessionSettings = new AmqpSessionSettings
            {
                Properties = new Fields(),
            };

            try
            {
                var amqpSession = new AmqpSession(_amqpConnection, amqpSessionSettings, AmqpIotLinkFactory.GetInstance());
                _amqpConnection.AddSession(amqpSession, new ushort?());
                await amqpSession.OpenAsync(timeout).ConfigureAwait(false);

                return(new AmqpIotSession(amqpSession));
            }
            catch (Exception e) when(!e.IsFatal())
            {
                Exception ex = AmqpIotExceptionAdapter.ConvertToIotHubException(e, _amqpConnection);

                if (ReferenceEquals(e, ex))
                {
                    throw;
                }
                else
                {
                    if (ex is AmqpIotResourceException)
                    {
                        _amqpConnection.SafeClose();
                        throw new IotHubCommunicationException(ex.Message, ex);
                    }
                    throw ex;
                }
            }
        }