3

We need to talk to a third-party system through HTTP POST requests - depending on the parameters of such requests, it happens that processing time exceeds 5 minutes.

We have no issue fetching the data with CURL or Postman in such cases, but for some reason our HttpClient implementation hangs/remains stuck. Even creating an ad-hoc Console Application to talk to the server results in the following behavior:

Request starts -> Wait until whatever timeout we have set on HttpClient is triggered -> Tieout Exception. In other words, it appears that the response being sent by the server is "ignored" and the component waits for timeout. We are able to successfully make request from the same machine by using CURL or Postman, so I doubt it's machine-dependent.

Of course, the timeout is higher than the server response time, and we can confirm that the server is actually processing & sending the response through.

Size of the response payload does not change significantly with processing time - it seems that the only variable here is processing time itself.

Any clue / ideas for troubleshooting this?

Editing to add code (.NET 4.5):

var content = new StringContent("{}", Encoding.UTF8, "application/json");
client.Timeout = TimeSpan.FromMinutes(8);

var response = client.PostAsync("https://destination.service.com/myendpoint", content).Result;
var responseString = response.Content.ReadAsStringAsync().Result;

This is from a small testing C# Console Application that we created to troubleshoot the issue - really nothing too fancy as you can see.

5
  • have you tried other libraries like Restsharp? restsharp.org Commented Jul 23, 2019 at 7:18
  • 4
    Can we see your code? It's hard to help a coding problem without the code. Commented Jul 23, 2019 at 7:18
  • Is this net or netcore ? If netcore, take a look at this link learn.microsoft.com/en-us/dotnet/standard/… Commented Jul 23, 2019 at 7:26
  • I have amended my question to include the code (running on Windows Server 2016 on .NET 4.5) Commented Jul 23, 2019 at 7:29
  • Consider activating network tracig to help you zero in on where it is stuck. It'll show you, for example, if it is stuck trying to authenticate to your proxy server, or if it is trying to check a CRL, etc Commented Jul 23, 2019 at 7:49

3 Answers 3

1

I think the problem is that forcing an async call to be synchronous will block the thread during the process. The call will needs a thread to complete the operation, which will fails since no threads are available.

This is the case if called from UI Thread or from threadpool thread (a deadlock can also happens in main thread), especially when the workload is really high like yours.

Try the async version:

var response = await client.PostAsync("https://destination.service.com/myendpoint", content);
var responseString = await response.Content.ReadAsStringAsync();
Sign up to request clarification or add additional context in comments.

Comments

0

check with async, like this:

private async Task<string> CallService()
{
    HttpClient client = new HttpClient();
    var content = new StringContent("{}", Encoding.UTF8, "application/json");
    client.Timeout = TimeSpan.FromMinutes(8);

    var response = await client.PostAsync(url, content);
    string data = await response.Content.ReadAsStringAsync();

    return data;
}

2 Comments

You're missing a return data; at the end, for completeness
Your mean that ALFA? return data;
0

Can you edit your question to include the method signature of the console APP.

For me the signature and code should be changed to

public async Task<string> PostData()
{

    var content = new StringContent("{}", Encoding.UTF8, "application/json");
    client.Timeout = TimeSpan.FromMinutes(8);

    var response = await client.PostAsync("https://destination.service.com/myendpoint", content);
    var responseString = await response.Content.ReadAsStringAsync();
    return responseString;

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.