我有一个异步方法:
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;
}
我认为这会导致僵局.
EDIT个
像上面这样调用方法会使调用永远不会结束.(该方法不再达到终点)