我在控制台应用程序中尝试过它
public async Task<string> Getsmth(string a, string b, string c, HttpClient client)
{
string str = "call to node.js"
var response = await client.GetStringAsync(str);
return response;
}
在控制台应用程序中调用它的工作量很大,但是在web api中调用相同的代码之后它就陷入了等待线
[Route("api/Getsmth/{a}/{b}/{c}")]
public string Get(string a, string b, string c)
{
HttpClient client = new HttpClient();
var r = Getsmth(a, b, c, client);
return r.Result;
}
同步调用它(没有异步/等待)一切正常。什么似乎是问题?!如何让它异步工作?
答案 0 :(得分:0)
尝试更改api操作以返回Task<IHttpActionResult>
,然后等待Getsmth
,如下所示:
[Route("api/Getsmth/{a}/{b}/{c}")]
public async Task<IHttpActionResult> Get(string a, string b, string c)
{
HttpClient client = new HttpClient();
var r = await Getsmth(a, b, c, client);
return Ok(r);
}
您也可以从Task<string>
返回Get
,然后只返回return r
而不是Ok(r)
:
[Route("api/Getsmth/{a}/{b}/{c}")]
public async Task<string> Get(string a, string b, string c)
{
HttpClient client = new HttpClient();
var r = await Getsmth(a, b, c, client);
return r;
}