0
我有一個屬性。在getter中,我想調用一個異步方法。請看看代碼,我也標誌着意見: -與C中的屬性異步#
public string AuthToken
{
get
{
TimeSpan timediff = App.Locator.Login.TokenExpire_On.ToLocalTime() - DateTime.Now.ToLocalTime();
if (timediff.TotalMinutes < 55)
App.Locator.Login.GetRefreshToken();
// before return the value i want to complete the work of GetRefreshToken(), GetRefreshToken is a async method.
return AppSettings.GetValueOrDefault<string>(AuthTokenKey, AuthTokenDefault);
}
set
{
AppSettings.AddOrUpdateValue<string>(AuthTokenKey, value);
}
}
public async Task GetRefreshToken()
{
if (string.IsNullOrEmpty(this.AuthToken))
return;
AzureRefreshToken refreshToken = await UserProfileService.RefreshToken(this.AuthToken);
Settings.AuthToken = refreshToken.RefreshToken;
TokenExpire_On = DateTime.Now.AddHours(1);
}
可能的重複[如何從getter或setter調用異步方法?](http://stackoverflow.com/questions/6602244/how-to-call-an-async-method-from-a-getter -or-setter) – Sinatr
用不同的方式思考你的問題:如果沒有任何事情在它過期後才調用「AuthToken」會怎麼樣?目前的基於財產的解決方案假定在該5分鐘時間窗口即將到期之前將進行呼叫。國際海事組織將有更新循環更合適。 –