28

await httpClient.SendAsync(httpContent) is not responding though I found no error in code/url its still getting hang. Please suggest/help.

My code as follows:

public async Task<string> Get_API_Result_String(string url, List<KeyValuePair<string, string>> parameters)
{
    string res = "";

    try
    {
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        //Prepare url
        Uri mainurl = new Uri(settings[FSAPARAM.UserSettingsParam.SERVERNAME].ToString());
        Uri requesturl = new Uri(mainurl, url);

        var httpClient = new HttpClient();
        var httpContent = new HttpRequestMessage(HttpMethod.Post, requesturl);
        // httpContent.Headers.ExpectContinue = false;

        httpContent.Content = new FormUrlEncodedContent(parameters);

        HttpResponseMessage response = await httpClient.SendAsync(httpContent);

        var result = await response.Content.ReadAsStringAsync();
        res = result.ToString();

        response.Dispose();
        httpClient.Dispose();
        httpContent.Dispose();
    }
    catch (Exception ex)
    {
        Logger l = new Logger();
        l.LogInfo("Get_API_Result_String: "+ url + ex.Message.ToString());
        ex = null;
        l = null;
    }

    return res;
}

Calling it in another class as follows:

NetUtil u = new NetUtil();
string result = await u.Get_API_Result_String(Register_API, values);
u = null;
Luiso
  • 4,173
  • 2
  • 37
  • 60
Priti
  • 336
  • 1
  • 3
  • 15

5 Answers5

50

I predict that further up your call stack, you are calling Wait or Result on a returned Task. This will cause a deadlock that I explain fully on my blog.

To summarize, await will capture a context and use that to resume the async method; on a UI application this is a UI thread. However, if the UI thread is blocked (in a call to Wait or Result), then that thread is not available to resume the async method.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • Yes, I checked it. I am calling every upper level method with await. – Priti Oct 31 '13 at 10:58
  • I tried to put exact same scenrio in testApp where its working fine. ny current project has lots of code so I am tracing carefully everywhere to find deadlock. Thanks – Priti Oct 31 '13 at 11:45
  • I have used async with Application_Launching because I have to call async method in Application_Launching I think this is creating issue. Can you please help. – Priti Nov 01 '13 at 06:55
16

this worked for me:

httpClient.SendAsync(httpContent).ConfigureAwait(false);
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
pata
  • 959
  • 2
  • 18
  • 35
1

I just removed await and just used as below and it worked:

var result = httpClient.SendAsync(httpContent).Result;

But that is not a good practice. As Nikola mentioned, we shouldn't mix sync and async calls.
I changed the calling method to async and problem got resolved.

Andrew Myers
  • 2,754
  • 5
  • 32
  • 40
1

This is OK on mine

  var response = httpClient.SendAsync(request);
  var responseResult = response.Result;

  if (responseResult.IsSuccessStatusCode)
  {
         var result = responseResult.Content.ReadAsStringAsync().Result;
         return result;
  }
Pit
  • 395
  • 2
  • 11
0

Just got this bug when returning compressed gzip data. It's a very rare case as 99% times with different input data everything is fine. Had to switch to HttpWebRequest.

kamyker
  • 311
  • 2
  • 6