-2

There was a resolved question for System.Net.Http.HttpClient. what-is-the-overhead-of-creating-a-new-httpclient-per-call-in-a-webapi-client

The answer is: Reuse HttpClient instance instead of creating new instance.

Now I am using Windows.Web.Http.HttpClient in an UWP application.

Regarding Windows.Web.Http.HttpClient ,the answer is the same ?

Jasper
  • 11
  • 1
  • 1
  • 2
    The fact that it's UWP does not change the way the library works, so yes, it's the same answer. – Jesse Jan 25 '20 at 07:18

1 Answers1

0

Reuse or create new Windows.Web.Http.HttpClient

The short answer yes, it could be reused. We often make single-instance pattern to package HttpClient. You could keep an instance of HttpClient for the lifetime of your application for each distinct API. And the following is thread safe.

public class HttClientProvider
{
    private static HttpClient _instance = null;
    private static readonly object _instanceLock = new object(); 
    public static HttpClient Instance
    {
        get
        {
            if (null == _instance)
            {
                lock (_instanceLock)
                {
                    if (null == _instance)
                    {
                        _instance = new HttpClient();
                    }
                }
            }
            return _instance;
        }
    }
}
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36