locked
HttpClient.PostAsync doesn't work when awaited RRS feed

  • Question

  • User17482 posted

    Can anyone explain the following?:

        HttpResponseMessage response = null;
    
        var jsonRequest = JsonConvert.SerializeObject(obj);
    
        try 
        {
            var content  = new StringContent(jsonRequest, Encoding.UTF8, "text/json");
    
            response = client.PostAsync(url, content).Result;
    
            //response = await client.PostAsync(url, content);
    
        }
        catch(Exception e)
        {
            Console.WriteLine(e.Message);
        }
    

    The uncommented line (which blocks) works fine. The commented line, which awaits the response, hangs indefinitely. client is just a System.Net.Http.HttpClient.

    Monday, May 26, 2014 2:22 AM

All replies

  • User17482 posted

    Ok, discovered the answer the hard way after 2 days of tracing and breakpointing. In a nutshell, you can't call an asynchronous method

    await asyncmethod2()
    

    from a method that blocks

    method1().Result
    

    as it causes a deadlock to occur when method2 attempts to return execution to the caller.

    Monday, May 26, 2014 6:13 AM
  • User50873 posted

    Hi Richard, Iis there a way tu await PostAsync?

    Thursday, August 28, 2014 11:01 PM
  • User82885 posted

    This article does a very good job explaining what happened to you: http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

    In short, don't .Result or .Wait a Promise task (a Task returned by an "async" method containing an "await") because it will block the activation of the "await" in the calling context, where it is meant to resume.

    Thursday, January 8, 2015 5:07 PM
  • User105524 posted

    Thanks Richard Hopkins. You saved my days

    Friday, February 27, 2015 10:25 PM
  • User345042 posted

    HttpResponseMessage response = null; //Declaring an http response message

    var jsonRequest = JsonConvert.SerializeObject(obj);// converting the obj into  a JSON object
    
    try 
    {
        var content  = new StringContent(jsonRequest, Encoding.UTF8, "text/json");
    
        response = client.PostAsync(url, content).Result;// storing the results of posts in response
    
        //response = await client.PostAsync(url, content);
    
    }
    catch(Exception e)
    {
        Console.WriteLine(e.Message);
    }h
    
    Saturday, October 14, 2017 12:00 PM
  • User353435 posted

    I had a blocking problem with my Task<> MakeRequest method.. it Posts a file and is supposed to assign a label with the response. This line was being called in a button click handler:

    MakeRequest(file).Wait();

    to fix it I had to change the method return type toTask<string>and use this line of code instead

    myLabel.Text = await MakeRequest(file);

    Thursday, October 19, 2017 12:03 PM
  • User366563 posted

    @RichardHopkins said: Ok, discovered the answer the hard way after 2 days of tracing and breakpointing. In a nutshell, you can't call an asynchronous method

    await asyncmethod2()
    

    from a method that blocks

    method1().Result
    

    as it causes a deadlock to occur when method2 attempts to return execution to the caller.

    Worked for me too. Thanks

    Friday, March 23, 2018 12:28 PM