0

I have this code:

var content = new ByteArrayContent(Encoding.ASCII.GetBytes(String.Concat("GET\t", _licencia, "\n")));
var recibido = await _client.PostAsync(fullUri, content);
var response = recibido.StatusCode;
this.IsOnline = response == HttpStatusCode.OK;
string contenido = await recibido.Content.ReadAsStringAsync();
if (this.IsOnline)
{
    string data = contenido.TrimEnd();
    List<string> transacciones = String.IsNullOrEmpty(data) ? null : data.Split('\n').ToList();
    var guardadas = ProcessTransactions(transacciones);

    if (guardadas != null)
    {
        StringBuilder sb = new StringBuilder(String.Concat("OK\t", _licencia, "\n"));
        content = new ByteArrayContent(Encoding.ASCII.GetBytes(sb.ToString()));
        recibido = await _client.PostAsync(fullUri, content);

Please see the first PostAsync call:

var content = new ByteArrayContent(Encoding.ASCII.GetBytes(String.Concat("GET\t", _licencia, "\n")));
var recibido = await _client.PostAsync(fullUri, content);

In this case, content length is only 34 bytes. So far, so good. Server receives the requirement correctly. But now see the second PostAsync call:

StringBuilder sb = new StringBuilder(String.Concat("OK\t", _licencia, "\n"));
content = new ByteArrayContent(Encoding.ASCII.GetBytes(sb.ToString()));
            recibido = await _client.PostAsync(fullUri, content);

In this case, content length is 33 bytes. However, that call gets blocked and finally it throws Task Cancelled Exception. This is because of the timeout, which is defined by the HttpClient to be 1 minute 40 seconds.

Why the second call gets blocked? How can I solve it?

Thanks Jaime

jstuardo
  • 3,901
  • 14
  • 61
  • 136
  • If the first call still not finished the second call will wait for it, this will cause the block – ElasticCode Apr 07 '18 at 00:39
  • First call is finished. In fact, the server is another Visual Studio project in my PC. when the first PostAsync call executes, debugger of the Web project takes control. Then I step over all instructions in the Web project until a "return Content()" is executed. After that, debugger of the first project takes control again just after the first PostAsync code. Then, I step over instructions of the first project until it reaches the second PostAsync call. After stepping over it, debugger of the web project does not take control, blocking the call in the first project until the timeout occurs. – jstuardo Apr 07 '18 at 00:51
  • I discovered that if I place the second PostAsync before ProcessTransactions call, it works. ProcessTransactions method only saves some data into a database (in a Task.Run action), nothing else. How may it be blocking the httpclient call? – jstuardo Apr 07 '18 at 01:15
  • I think the task is causing httpclient to block, because, when ProcessTransactions returns, there are several threads still running, so i finally created an async ProcessTransactions method and I called it with await. i think this way the problem will be solved. – jstuardo Apr 07 '18 at 01:36
  • I think its related to `await` operator, take a look [here also](https://stackoverflow.com/a/49702500/3134112) – ElasticCode Apr 07 '18 at 02:11

0 Answers0