我有一个异步方法:
public async Task<bool> ValidateRequestAsync(string userName, string password)
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(url);
string stringResponse = await response.Content.ReadAsStringAsync();
return bool.Parse(stringResponse);
}
}
我像这样调用这个方法:
bool isValid = await ValidateRequestAsync("user1", "pass1");
在同步方法中,我可以调用相同的方法,而不使用await
关键字吗?
例如:
public bool ValidateRequest(string userName, string password)
{
return ValidateRequestAsync(userName, password).Result;
}
我认为这会导致死锁。
编辑
像上面那样调用该方法会使调用永远不会结束。(该方法不再到达结尾)