简体   繁体   English

为什么我的WebApi客户端只能发出一个GET请求?

[英]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.

相关问题 为什么我的服务器无法响应Web浏览器对客户端的请求? - Why my server can not respond to the client's request from the webbrowser? 如何对不同的数据集发出单个获取请求? - How can make a single get request for different sets of data? 单客户端(消费者)的webapi安全性 - webapi security for single client (consumer) 为什么我的 SPA 无法通过重定向到 https://login.microsoftonline.com 的 WebAPI 进行身份验证? - Why can my SPA not get authenticated through WebAPI which redirects to https://login.microsoftonline.com? 等待异步请求webapi 2客户端 - Await in async request webapi 2 client 无法在WebApi OData中调用单个查询GET - Can't invoke single query GET in WebApi OData 当我使用参数向Web API请求时,为什么会得到“全部获取”而不是“单一获取” - Why I get Get all instead of single when I make a request to a web API with a parameter 不能在WebAPI get请求中使用object作为参数 - Can't use object as parameter in WebAPI get request 进行申请,使其成为唯一可以连接到我的WebAPI的应用程序 - Making an appliction so it is the only application that can connect to my WebAPI Webapi身份验证,仅使用客户端ouath - Webapi authentication, using client side ouath only
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM