1

I know many have discussed this before and agreed that Httpclient is best to be instantiated once and used for the entire application. I have some inquiries about this point.

Let's say i have a business layer class with static Httpclient variable (with double check lock) and a web page creates an object of the business layer class.

Class code:

  public class BL
{
    //Lock object for double lock check
    private static readonly object padlock = new object();
    public static HttpClient httpClient;

    public BL()
    {
        if (httpClient == null)
        {
            lock (padlock)
            {
                if (httpClient == null)
                {
                    httpClient = new HttpClient();
                }

            }
        }
    }

    public async Task<HttpResponseMessage> GetData(String URL)
    {
       return await httpClient.GetAsync(URL).ConfigureAwait(false);
    }
}

Page code

protected void Page_Load(object sender, EventArgs e)
    {
        BL blObj = new BL();
        HttpResponseMessage response = blObj.GetData("http://....");
    }
  1. Static variables keep alive for the whole application lifetime. That means the static httpclient will be alive as long as the host IIS and web app are alive (even after the lifecycle of the page that created BL object ends) or just alive till page is disposed?

  2. If multiple concurrent pages e.g 500 created BL object and used the static httpclient, how 500 users will be served through just one static httpclient that only enables one http connection with the server? I can't get that one.

  3. Is there a reliable visual studio tool to test load and performance of the above code?

7
  • 1
    it wont - if you need more than 1 it isnt necessarily wrong to have more than one, its the creation and destruction of them that seems to be the crux of the situation - you could make a pool of them and have a queue for the pool to process. And VS has many built in profiling tools, knock out some code to test your ideas
    – BugFinder
    Feb 28, 2019 at 8:34
  • @BugFinder Thank you. Could you please specify the behavior you mean in your answer "it wont"?- How to know if I need more than one instance or not (the system users are 6000 maximum and concurrency will not exceed 1000 user )?- Appreciate if you could refer me to a working example
    – Ah.R
    Feb 28, 2019 at 8:56
  • 1
    The double-checked locking is entirely pointless here - just do public static HttpClient httpClient = new HttpClient()
    – canton7
    Feb 28, 2019 at 9:15
  • "one static httpclient that only enables one http connection with the server" - where did you get this from? I can't find any evidence for it.
    – canton7
    Feb 28, 2019 at 9:17
  • @canton7 Thank you for reply. Could you explain why pointless? - Regarding static httpclient, yes i think i misunderstand and searched without getting answer. Could you explain how static httpclient will behave for concurrent multiple requests?
    – Ah.R
    Feb 28, 2019 at 9:23

1 Answer 1

0
  1. Static field lives until the AppDomain is disposed, that is because they do not belong to the object instance hence they won't be CGed. in IIS situation, depending on your setting in IIS, it will live forever if the application pool is not recycled.

  2. Yes, that static client obj will server for the entire 500 concurrent request, but you need to be very careful that if HttpClient is a thread safe object. If not, you'd better try this way: Create a pool of HttpClient, tie a status to every HttpClient object in the pool, pick up idle HttpClient in the pool while BL object is trying to get data

  3. VS got a lot performance tools, MS released a performance tool called VSPerfASPNetCmd to monitor CPU usage, memory usage, you can refer the below page for details https://learn.microsoft.com/en-us/visualstudio/profiling/vsperfaspnetcmd?view=vs-2017
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.