Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Forms HttpClient PostAsync no response, no error message

I have this code:

using (var client = new HttpClient())
{
    var values = new Dictionary<string, string>
        {
            {"site_id","001"},
            {"apikey","abc01201az1024"},
            {"trans_id","45364136"},
        };

    // Get the parameters in the url encoded format
    var content = new FormUrlEncodedContent(values);

    //Send request
    var response = await client.PostAsync(url, content);

    DataRoot<Transaction> outPut = null;
    if (response.IsSuccessStatusCode)
    {
        //Get Response
        var result = await response.Content.ReadAsStringAsync();
        JsonConvert.DeserializeObject<DataRoot<Transaction>>(result);
    }

    return outPut;
}

In the debug mode at this stage, the code does not produce any response, no error code but stops running:

//Send request
var response = await client.PostAsync(url, content);
like image 537
Jmocke Avatar asked Oct 31 '25 20:10

Jmocke


1 Answers

Using await like this, can end up in a deadlock.

You can use ConfigureAwait(false) in async methods for preventing such a deadlock.

Update code to:

var response = await client.PostAsync(url, content).ConfigureAwait(false);

This will solve the issue.

like image 133
Habeeb Avatar answered Nov 03 '25 10:11

Habeeb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!