现在想对SendAsync变成同步方法。
通过SendAsync("http://www.baidu.com").Result;
却一直在等待
public ActionResult Test() { var s = SendAsync("http://www.baidu.com").Result; return Content(s.Msg); } public async Task<AjaxResponase> SendAsync(string url) { var http = new WebApiClient(); return await http.GetAsync<AjaxResponase>(url); } private async Task<TResult> GetAsync<TResult>(string url, int? timeout = null) where TResult : class, new() { using (var client = new HttpClient()) { using (var response = await client.GetAsync(url)) { if (!response.IsSuccessStatusCode) { throw new Exception("Could not made request to " + url + "! StatusCode: " + response.StatusCode + ", ReasonPhrase: " + response.ReasonPhrase); } var ajaxResponse = JsonString2Object<TResult>(await response.Content.ReadAsStringAsync()); return ajaxResponse; } } }
ASP.NET里调用的这个代码?,不死锁才怪。
没明白你说的.能详细吗
@jakeys: http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html,如果你想同步,请不要调用异步的方法,直接调用同步的方法即可。
Test方法改成试试:
public async Task<ActionResult> Test() { var s = await SendAsync("http://www.baidu.com").Result; return Content(s.Msg); }
谢谢你的回答,这样是可以的,但是我想把SendAsync变成同步的方法,在不改动GetAsync的前提下.怎么做呢
试试回调