private RenewTokenFuncAsync GetTokenRenewer(IAccessToken accessToken) { #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously RenewTokenFuncAsync renewer = async(Object state, CancellationToken cancellationToken) => #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously { var tokenStr = GetTokenStrFromAccessToken(accessToken); return(new NewTokenAndFrequency(tokenStr, new TimeSpan(0, 1, 0))); }; return(renewer); }
/// <summary> /// Create an instance of <see cref="TokenCredential"/>. /// </summary> /// <param name="initialToken">Initial value of the token credential.</param> /// <param name="periodicTokenRenewer">If given, this delegate is called periodically to renew the token credential.</param> /// <param name="state">A state object is passed to the periodicTokenRenewer every time it is called.</param> /// <param name="renewFrequency">If periodicTokenRenewer is given, user should define a frequency to call the periodicTokenRenewer.</param> public TokenCredential(String initialToken, RenewTokenFuncAsync periodicTokenRenewer, Object state, TimeSpan renewFrequency) { this.token = initialToken; // if no renewer is given, then the token will not be updated automatically. if (periodicTokenRenewer == null) { return; } this.renewTokenFuncAsync = periodicTokenRenewer; this.renewFrequency = renewFrequency; // when "new Timer(...)" is called, it might call RenewTokenAsync before even being assigned to timer, if renewFrequency is very close to 0. // since RenewTokenAsync refers to timer, we need to make sure that before it is invoked, timer is defined. this.timer = new Timer(RenewTokenAsync, state, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan); this.timer.Change(this.renewFrequency, Timeout.InfiniteTimeSpan); this.cancellationTokenSource = new CancellationTokenSource(); }