async Task RenewCBSTokenAsync(ActiveClientLinkObject activeClientLinkObject)
        {
            try
            {
                AmqpCbsLink cbsLink = activeClientLinkObject.Connection.Extensions.Find <AmqpCbsLink>() ?? new AmqpCbsLink(activeClientLinkObject.Connection);

                MessagingEventSource.Log.AmqpSendAuthenticanTokenStart(activeClientLinkObject.EndpointUri, activeClientLinkObject.Audience, activeClientLinkObject.Audience, activeClientLinkObject.RequiredClaims);

                activeClientLinkObject.AuthorizationValidUntilUtc = await cbsLink.SendTokenAsync(
                    this.cbsTokenProvider,
                    activeClientLinkObject.EndpointUri,
                    activeClientLinkObject.Audience,
                    activeClientLinkObject.Audience,
                    activeClientLinkObject.RequiredClaims,
                    ActiveClientLinkManager.SendTokenTimeout).ConfigureAwait(false);

                this.SetRenewCBSTokenTimer(activeClientLinkObject);

                MessagingEventSource.Log.AmqpSendAuthenticanTokenStop();
            }
            catch (Exception e)
            {
                // failed to refresh token, no need to do anything since the server will shut the link itself
                MessagingEventSource.Log.AmqpSendAuthenticanTokenException(this.clientId, e);

                this.ChangeRenewTimer(activeClientLinkObject, Timeout.InfiniteTimeSpan);
            }
        }
 void ChangeRenewTimer(ActiveClientLinkObject activeClientLinkObject, TimeSpan dueTime)
 {
     if (activeClientLinkObject is ActiveSendReceiveClientLink)
     {
         this.sendReceiveLinkCBSTokenRenewalTimer.Change(dueTime, Timeout.InfiniteTimeSpan);
     }
     else
     {
         this.requestResponseLinkCBSTokenRenewalTimer.Change(dueTime, Timeout.InfiniteTimeSpan);
     }
 }
        void SetRenewCBSTokenTimer(ActiveClientLinkObject activeClientLinkObject)
        {
            if (activeClientLinkObject.AuthorizationValidUntilUtc < DateTime.UtcNow)
            {
                return;
            }

            TimeSpan interval = activeClientLinkObject.AuthorizationValidUntilUtc.Subtract(DateTime.UtcNow) - ActiveClientLinkManager.TokenRefreshBuffer;

            this.ChangeRenewTimer(activeClientLinkObject, interval);
        }
        void SetRenewCbsTokenTimer(ActiveClientLinkObject activeClientLinkObject)
        {
            if (activeClientLinkObject.AuthorizationValidUntilUtc < DateTime.UtcNow)
            {
                return;
            }

            var interval = activeClientLinkObject.AuthorizationValidUntilUtc.Subtract(DateTime.UtcNow) - ActiveClientLinkManager.TokenRefreshBuffer;

            interval = TimeoutHelper.Min(interval, ActiveClientLinkManager.MaxTokenRefreshTime);

            this.ChangeRenewTimer(activeClientLinkObject, interval);
        }
        async Task RenewCbsTokenAsync(ActiveClientLinkObject activeClientLinkObject)
        {
            try
            {
                var      cbsLink = activeClientLinkObject.Connection.Extensions.Find <AmqpCbsLink>() ?? new AmqpCbsLink(activeClientLinkObject.Connection);
                DateTime cbsTokenExpiresAtUtc = DateTime.MaxValue;

                foreach (var resource in activeClientLinkObject.Audience)
                {
                    MessagingEventSource.Log.AmqpSendAuthenticationTokenStart(activeClientLinkObject.EndpointUri, resource, resource, activeClientLinkObject.RequiredClaims);

                    await this.retryPolicy.RunOperation(
                        async() =>
                    {
                        cbsTokenExpiresAtUtc = TimeoutHelper.Min(
                            cbsTokenExpiresAtUtc,
                            await cbsLink.SendTokenAsync(
                                this.cbsTokenProvider,
                                activeClientLinkObject.EndpointUri,
                                resource,
                                resource,
                                activeClientLinkObject.RequiredClaims,
                                ActiveClientLinkManager.SendTokenTimeout).ConfigureAwait(false));
                    }, ActiveClientLinkManager.SendTokenTimeout).ConfigureAwait(false);

                    MessagingEventSource.Log.AmqpSendAuthenticationTokenStop();
                }

                activeClientLinkObject.AuthorizationValidUntilUtc = cbsTokenExpiresAtUtc;
                this.SetRenewCbsTokenTimer(activeClientLinkObject);
            }
            catch (Exception e)
            {
                // failed to refresh token, no need to do anything since the server will shut the link itself
                MessagingEventSource.Log.AmqpSendAuthenticationTokenException(this.clientId, e);

                this.ChangeRenewTimer(activeClientLinkObject, Timeout.InfiniteTimeSpan);
            }
        }