I use async method follow this code:
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();
}
and
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();
}
}
So after call postJsonAsync method and after that don`t fill publicToken if I use wait for this vs crash and closed. Do you have idea for this ?