0

I wrote a simple client for my web service with Web Api as explained in this tutorial:

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.

But the program is totally blocked before executing the second request (GET table list); 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?

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 ...
    }
}
tuncalik
  • 1,124
  • 1
  • 14
  • 20
  • Sounds like a deadlock but I don't see exactly what the cause would be. What type of application is this client? How is the method containing your code sample called? – Ryan M Jul 23 '14 at 20:46
  • This cliend is a console application (.NET 4.5) as explained in this tutorial: http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client As shown in the tutorial, the statement "RunAsync().Wait()" in Main() calls "static async Task RunAsync()", which envelopes the using{} block displayed here. – tuncalik Jul 23 '14 at 21:05

1 Answers1

2

Looking at the following SO question and its accepted answer (maybe not directly relevant but still useful in its own right):

it might help if you replace your calls to GetAsync() as follows:

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.

Community
  • 1
  • 1
djikay
  • 10,450
  • 8
  • 41
  • 52