Пример #1
0
        protected override async Task OpenLinkAsync(AmqpObject link, IotHubConnectionString connectionString, string audience, TimeSpan timeout, CancellationToken token)
        {
            var timeoutHelper = new TimeoutHelper(timeout);

            token.ThrowIfCancellationRequested();
            try
            {
                // this is a device-scope connection string. We need to send a CBS token for this specific link before opening it.
                var iotHubLinkTokenRefresher = new IotHubTokenRefresher(this.FaultTolerantSession.Value, connectionString, audience);

                if (this.iotHubTokenRefreshers.TryAdd(link, iotHubLinkTokenRefresher))
                {
                    link.SafeAddClosed((s, e) =>
                    {
                        if (this.iotHubTokenRefreshers.TryRemove(link, out iotHubLinkTokenRefresher))
                        {
                            iotHubLinkTokenRefresher.Cancel();
                        }
                    });

                    // Send Cbs token for new link first
                    // This will throw an exception if the device is not valid or if the token is not valid
                    await iotHubLinkTokenRefresher.SendCbsTokenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false);
                }

                token.ThrowIfCancellationRequested();
                // Open Amqp Link
                await link.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false);
            }
            catch (Exception exception) when(!exception.IsFatal())
            {
                link.SafeClose(exception);
                throw;
            }
        }
        protected override async Task <AmqpSession> CreateSessionAsync(TimeSpan timeout, CancellationToken cancellationToken)
        {
            var timeoutHelper = new TimeoutHelper(timeout);

            if (this.iotHubTokenRefresher != null)
            {
                this.iotHubTokenRefresher.Cancel();
            }

            AmqpSession amqpSession = await base.CreateSessionAsync(timeoutHelper.RemainingTime(), cancellationToken);

#if !WINDOWS_UWP
            if (this.AmqpTransportSettings.ClientCertificate == null)
            {
#endif
            this.iotHubTokenRefresher = new IotHubTokenRefresher(
                amqpSession,
                this.ConnectionString,
                this.ConnectionString.AmqpEndpoint.AbsoluteUri
                );

            // Send Cbs token for new connection first
            await this.iotHubTokenRefresher.SendCbsTokenAsync(timeoutHelper.RemainingTime());

#if !WINDOWS_UWP
        }
#endif

            return(amqpSession);
        }
        protected override async Task <AmqpSession> CreateSessionAsync(TimeSpan timeout, CancellationToken cancellationToken)
        {
            var timeoutHelper = new TimeoutHelper(timeout);

            this.iotHubTokenRefresher?.Cancel();

            AmqpSession amqpSession = await base.CreateSessionAsync(timeoutHelper.RemainingTime(), cancellationToken).ConfigureAwait(false);

            if (this.AmqpTransportSettings.ClientCertificate == null)
            {
                this.iotHubTokenRefresher = new IotHubTokenRefresher(
                    amqpSession,
                    this.ConnectionString,
                    this.ConnectionString.AmqpEndpoint.AbsoluteUri
                    );

                // Send Cbs token for new connection first
                try
                {
                    await this.iotHubTokenRefresher.SendCbsTokenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false);
                }
                catch (Exception exception) when(!exception.IsFatal())
                {
                    amqpSession?.Connection.SafeClose();

                    throw;
                }
            }

            return(amqpSession);
        }
        protected override async Task<AmqpSession> CreateSessionAsync(TimeSpan timeout)
        {
            var timeoutHelper = new TimeoutHelper(timeout);

            if (this.iotHubTokenRefresher != null)
            {
                this.iotHubTokenRefresher.Cancel();
            }

            AmqpSession amqpSession = await base.CreateSessionAsync(timeoutHelper.RemainingTime());

#if !WINDOWS_UWP
            if (this.AmqpTransportSettings.ClientCertificate == null)
            {
#endif
                this.iotHubTokenRefresher = new IotHubTokenRefresher(
                   amqpSession,
                   this.ConnectionString,
                   this.ConnectionString.AmqpEndpoint.AbsoluteUri
                   );

                // Send Cbs token for new connection first
                await this.iotHubTokenRefresher.SendCbsTokenAsync(timeoutHelper.RemainingTime());
#if !WINDOWS_UWP
            }
#endif

            return amqpSession;
        }
Пример #5
0
        protected override async Task OpenLinkAsync(AmqpObject link, IotHubConnectionString connectionString, string audience, TimeSpan timeout)
        {
            var timeoutHelper = new TimeoutHelper(timeout);

            try
            {
                // this is a device-scope connection string. We need to send a CBS token for this specific link before opening it.
                var iotHubLinkTokenRefresher = new IotHubTokenRefresher(
                    this.FaultTolerantSession.Value,
                    connectionString,
                    audience
                    );

                if (this.iotHubTokenRefreshers.TryAdd(link, iotHubLinkTokenRefresher))
                {
                    link.SafeAddClosed((s, e) =>
                    {
                        if (this.iotHubTokenRefreshers.TryRemove(link, out iotHubLinkTokenRefresher))
                        {
                            iotHubLinkTokenRefresher.Cancel();
                        }
                    });

                    // Send Cbs token for new link first
                    await iotHubLinkTokenRefresher.SendCbsTokenAsync(timeoutHelper.RemainingTime());
                }

                // Open Amqp Link
                await link.OpenAsync(timeoutHelper.RemainingTime());
            }
            catch (Exception exception)
            {
                if (exception.IsFatal())
                {
                    throw;
                }

                link.SafeClose(exception);
                throw;
            }
        }
        protected override async Task<AmqpSession> CreateSessionAsync(TimeSpan timeout)
        {
            var timeoutHelper = new TimeoutHelper(timeout);

            if (this.iotHubTokenRefresher != null)
            {
                this.iotHubTokenRefresher.Cancel();
            }

            AmqpSession amqpSession = await base.CreateSessionAsync(timeoutHelper.RemainingTime());
            this.iotHubTokenRefresher = new IotHubTokenRefresher(
                amqpSession, 
                this.ConnectionString, 
                this.ConnectionString.AmqpEndpoint.AbsoluteUri
                );

            // Send Cbs token for new connection first
            await this.iotHubTokenRefresher.SendCbsTokenAsync(timeoutHelper.RemainingTime());

            return amqpSession;
        }
Пример #7
0
        protected override async Task <AmqpSession> CreateSessionAsync(TimeSpan timeout, CancellationToken cancellationToken)
        {
            var timeoutHelper = new TimeoutHelper(timeout);

            if (this.iotHubTokenRefresher != null)
            {
                this.iotHubTokenRefresher.Cancel();
            }

            AmqpSession amqpSession = await base.CreateSessionAsync(timeoutHelper.RemainingTime(), cancellationToken);

#if !WINDOWS_UWP && !PCL
            if (this.AmqpTransportSettings.ClientCertificate == null)
            {
#endif
            this.iotHubTokenRefresher = new IotHubTokenRefresher(
                amqpSession,
                this.ConnectionString,
                this.ConnectionString.AmqpEndpoint.AbsoluteUri
                );

            // Send Cbs token for new connection first
            try
            {
                await this.iotHubTokenRefresher.SendCbsTokenAsync(timeoutHelper.RemainingTime());
            }
            catch (Exception exception) when(!exception.IsFatal())
            {
                // initiate the process of closing this connection
                this.Release("unknown");

                throw;
            }

#if !WINDOWS_UWP && !PCL
        }
#endif

            return(amqpSession);
        }
        protected override async Task <AmqpSession> CreateSessionAsync(TimeSpan timeout)
        {
            var timeoutHelper = new TimeoutHelper(timeout);

            if (this.iotHubTokenRefresher != null)
            {
                this.iotHubTokenRefresher.Cancel();
            }

            AmqpSession amqpSession = await base.CreateSessionAsync(timeoutHelper.RemainingTime());

            this.iotHubTokenRefresher = new IotHubTokenRefresher(
                amqpSession,
                this.ConnectionString,
                this.ConnectionString.AmqpEndpoint.AbsoluteUri
                );

            // Send Cbs token for new connection first
            await this.iotHubTokenRefresher.SendCbsTokenAsync(timeoutHelper.RemainingTime());

            return(amqpSession);
        }
        protected override async Task OpenLinkAsync(AmqpObject link, IotHubConnectionString connectionString, string audience, TimeSpan timeout)
        {
            var timeoutHelper = new TimeoutHelper(timeout);
       
            try
            {
                // this is a device-scope connection string. We need to send a CBS token for this specific link before opening it.
                var iotHubLinkTokenRefresher = new IotHubTokenRefresher(
                    this.FaultTolerantSession.Value, 
                    connectionString, 
                    audience
                    );

                if (this.iotHubTokenRefreshers.TryAdd(link, iotHubLinkTokenRefresher))
                {
                    link.SafeAddClosed((s, e) =>
                    {
                        if (this.iotHubTokenRefreshers.TryRemove(link, out iotHubLinkTokenRefresher))
                        {
                            iotHubLinkTokenRefresher.Cancel();
                        }
                    });

                    // Send Cbs token for new link first
                    await iotHubLinkTokenRefresher.SendCbsTokenAsync(timeoutHelper.RemainingTime());
                }

                // Open Amqp Link
                await link.OpenAsync(timeoutHelper.RemainingTime());
            }
            catch (Exception exception)
            {
                if (exception.IsFatal())
                {
                    throw;
                }

                link.SafeClose(exception);
                throw;
            }
        }