async方法在使用GetResult()時,產生deadlock
原先的程式碼如下,產生deadlock
private void button1_click()
{
//產生deadlock
ProductUpdateInfo pdUpate = Query.GetAppUpdateInfo(TokenText.Text, 1598, "1.21.525.1").GetAwaiter().GetResult();
}
private async Task<T> QeuryActionGeneric<T>(string token, object queryObject)
{
T result = default(T);
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
Content = new StringContent(JsonConvert.SerializeObject(queryObject), Encoding.UTF8, "application/json")
};
using (var response = await Client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<T>(responseString);
}
return result;
}
修改後,加上.ConfigureAwait(false)
private async Task<T> QeuryActionGeneric<T>(string token, object queryObject)
{
T result = default(T);
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
Content = new StringContent(JsonConvert.SerializeObject(queryObject), Encoding.UTF8, "application/json")
};
using (var response = await Client.SendAsync(request).ConfigureAwait(false))
{
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
result = JsonConvert.DeserializeObject<T>(responseString);
}
return result;
}