-1

Problem:

I am using .NET Class HttpClient to make Requests to the endpoint URL.

My Code:

using (HttpClient apiClient1 = new HttpClient())
{
    apiClient.GetAsync(apiUrl).Result;
}

Problem Identified:

If I use using block, I'm opening multiple connections, leads to socketExceptions.

Changed My Above Code to:

public class RequestController: ApiController
{
    private static HttpClient apiClient = new HttpClient();

    [HttpPost]
    public  dynamic GetWebApiData([FromBody] ParamData params)
    {
           var resultContent = apiClient.GetAsync(apiUrl).Result.Content;
           return Newtonsoft.Json.JsonConvert.DeserializeObject<object>(resultContent.ReadAsStringAsync().Result);
    }
}

Result of the above code after making HttpClient as static is as follows:

  1. Only one Connection is established.

  2. For Each request, I'm looking for the 200 milliseconds reduction in Response Time.

What I Need:

I want to make conurrent calls atleast 50 calls to the end point with High-Speed response.

Kindly help me with this scenario.

5
  • You need to read up and understand the fundamentals of async/await. If you do, your question will be answered. Commented Dec 24, 2018 at 21:24
  • Also, how are you making the concurrent calls? Commented Dec 25, 2018 at 1:19
  • @GabrielLuci I'm calling through internal WebApi Method "GetWebAPidata()", which then initiates HttpClient Commented Dec 25, 2018 at 6:40
  • @IanKemp, My question will not be answered, unless and until we could apply best approach to this kind of generic scenario, where we need to make multiple EndPoint Requests with less time. I'm sorting out where the problem went wrong. That's why I asked the question in this forum, where someone help me to get rid off. It's just discussion. Sorry If I'm wrong. Commented Dec 25, 2018 at 6:42
  • Yes, but that method only makes one request. How are you calling it 50 times concurrently? That code is just as important. Commented Dec 25, 2018 at 13:47

2 Answers 2

8

Use the async API and stop calling .Result blocking calls.

public class RequestController: ApiController {
    private static HttpClient apiClient = new HttpClient();

    [HttpPost]
    public async Task<IHttpActionResult> GetWebApiData([FromBody] ParamData data) {
        var response = await apiClient.GetAsync(apiUrl);
        var resultContent = response.Content;
        var model = await resultContent.ReadAsAsync<dynamic>();
        return Ok(model);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

This reduces the time and now it is taking 300 - 400 ms to get the result. Is it the right time consumption as I verified it by pinging the IP Address of the server through ping {IPAddress}, it is taking 245 milliseconds 1 roundtrip. Is it feasible to wait for this 300 - 400ms delay? Also, how the ".Result" will block the calls? Could you explain?
A ping would obviously be faster as no work is being done. The time mentioned is reasonable but that is a matter of preference.
0

The default SSL connection idle timeout of HttpClient are 2 mins. So, after that you have to re handshake with the server. This maybe the root cause.

You could follow this article(https://www.stevejgordon.co.uk/httpclient-connection-pooling-in-dotnet-core) to extend the timeout. But the testing result from my side, I could only extend to 5 mins. After that, the connection will be closed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.