[英]Why can my WebApi client make only a single GET request?
I wrote a simple client for my web service with Web Api as explained in this tutorial: 如本教程所述,我为使用Web Api的Web服务编写了一个简单的客户端:
http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client
The first request (GET a table) is executed successfully. 第一个请求(获取表)已成功执行。 That is, table data is fetched from the web service and bound to TableContent object. 也就是说,表数据是从Web服务中获取的,并绑定到TableContent对象。
But the program is totally blocked before executing the second request (GET table list); 但是在执行第二个请求(GET表列表)之前,程序被完全阻塞了; nothing happens, not even an error message! 没有任何反应,甚至没有错误消息!
If I comment out the code section related with the first request (GET a table) the second request (GET table list) is executed successfully. 如果我注释掉与第一个请求(获取表)相关的代码段,则第二个请求(获取表列表)将成功执行。
What happens here? 这里会发生什么? Why can this client execute only a single GET request? 为什么此客户端只能执行一个GET请求?
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:56510/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response;
// GET a table from web service
response = await client.GetAsync("api/table/GetMatrixTable");
if (response.IsSuccessStatusCode)
{
var tblcont = await response.Content.ReadAsAsync<TableContent>();
// do things ...
}
// GET a table list from web service
response = await client.GetAsync("api/table/GetTableList");
if (response.IsSuccessStatusCode)
{
var TblContList = await response.Content.ReadAsAsync<IList<TableContent>>();
// do things ...
}
}
Looking at the following SO question and its accepted answer (maybe not directly relevant but still useful in its own right): 查看下面的SO问题及其接受的答案(可能不直接相关,但就其自身而言仍然有用):
it might help if you replace your calls to GetAsync()
as follows: 如果您按以下方式替换对GetAsync()
调用,则可能会有所帮助:
response = await client.GetAsync("api/table/GetMatrixTable",
HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
. . .
response = await client.GetAsync("api/table/GetTableList",
HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
I'm not able to test that so I can't take all the credit if that works. 我无法对此进行测试,因此如果可行的话,我将无法承担全部责任。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.