异步等待方法(等待响应)
async-await method (Wait for response)
我使用异步方法遵循以下代码:
public string TryForGetToken()
{
cache = getCache;
if (!cache.Contains(keyToken))
{
CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
TryForLogin();
cacheItemPolicy.AbsoluteExpiration = DateTime.Now.AddSeconds(expiredTime - 60);
CacheItem item = new CacheItem(keyToken, publicToken);
//cache.Add(item, cacheItemPolicy);
return publicToken;
}
else
return cache.Get(keyToken).ToString();
}
和
public async Task TryForLogin()
{
await _semaphore.WaitAsync();
try
{
AuthenticationData authenticationData = new AuthenticationData() { email = "zzzzzz", password = "zzzzzzzz" };
ResponseResult result = await httpServiceCaller.PostJsonAsync<AuthenticationData, ResponseResult>("http://sample.com/agent/login", authenticationData, new CancellationToken(), "Login");
publicToken = result.data.token.access_token;
expiredTime = result.data.token.expires_in;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
finally
{
_semaphore.Release();
}
}
所以在调用 postJsonAsync 方法之后不要填写 publicToken
如果我使用等待这个 vs 崩溃并关闭。你有这方面的想法吗?
仅仅是因为 TryForGetToken
在 TryForLogin
赋值给 publicToken
变量之前完成。您不是在等待从 TryForLogin 方法返回的任务。
您应该将 TryForGetToken
重写为 async Task<string>
然后 await TryForLogin()
我不完全确定你要什么,但你似乎错过了等待 TryForLogin
方法。
public async Task<string> TryForGetToken()
{
cache = getCache;
if (!cache.Contains(keyToken))
{
CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
await TryForLogin();
cacheItemPolicy.AbsoluteExpiration = DateTime.Now.AddSeconds(expiredTime - 60);
CacheItem item = new CacheItem(keyToken, publicToken);
//cache.Add(item, cacheItemPolicy);
return publicToken;
}
else
return cache.Get(keyToken).ToString();
}
我使用异步方法遵循以下代码:
public string TryForGetToken()
{
cache = getCache;
if (!cache.Contains(keyToken))
{
CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
TryForLogin();
cacheItemPolicy.AbsoluteExpiration = DateTime.Now.AddSeconds(expiredTime - 60);
CacheItem item = new CacheItem(keyToken, publicToken);
//cache.Add(item, cacheItemPolicy);
return publicToken;
}
else
return cache.Get(keyToken).ToString();
}
和
public async Task TryForLogin()
{
await _semaphore.WaitAsync();
try
{
AuthenticationData authenticationData = new AuthenticationData() { email = "zzzzzz", password = "zzzzzzzz" };
ResponseResult result = await httpServiceCaller.PostJsonAsync<AuthenticationData, ResponseResult>("http://sample.com/agent/login", authenticationData, new CancellationToken(), "Login");
publicToken = result.data.token.access_token;
expiredTime = result.data.token.expires_in;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
finally
{
_semaphore.Release();
}
}
所以在调用 postJsonAsync 方法之后不要填写 publicToken 如果我使用等待这个 vs 崩溃并关闭。你有这方面的想法吗?
仅仅是因为 TryForGetToken
在 TryForLogin
赋值给 publicToken
变量之前完成。您不是在等待从 TryForLogin 方法返回的任务。
您应该将 TryForGetToken
重写为 async Task<string>
然后 await TryForLogin()
我不完全确定你要什么,但你似乎错过了等待 TryForLogin
方法。
public async Task<string> TryForGetToken()
{
cache = getCache;
if (!cache.Contains(keyToken))
{
CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
await TryForLogin();
cacheItemPolicy.AbsoluteExpiration = DateTime.Now.AddSeconds(expiredTime - 60);
CacheItem item = new CacheItem(keyToken, publicToken);
//cache.Add(item, cacheItemPolicy);
return publicToken;
}
else
return cache.Get(keyToken).ToString();
}