title: Singleton httpclient vs creating new httpclient request
body: <p>I am trying to create layer for webservice using HttpClient in my <code>Xamarin.Forms</code> mobile app.</p>
  
  <ol>
  <li>without singlton pattern</li>
  <li>with singleton pattern</li>
  </ol>
  
  <p>in <strong>first</strong> approach i am creating new http client object in each new request made 
  by mobile applicaiton.</p>
  
  <p>here is my code</p>
  
  <pre><code>  public HttpClient GetConnection()
          {
  
              HttpClient httpClient = new HttpClient();
              httpClient.BaseAddress = new Uri(baseAddress); 
              httpClient.Timeout = System.TimeSpan.FromMilliseconds(timeout);
  
  
              return httpClient;
  
          }
  </code></pre>
  
  <p>post request code </p>
  
  <pre><code> public async Task&lt;TResult&gt; PostAsync&lt;TRequest, TResult&gt;(String url, TRequest requestData)
          {
              HttpClient client = GetConnection();
              String responseData = null;
              if (client != null)
              {
  
                  String serializedObject = await Task.Run(() =&gt; JsonConvert.SerializeObject(requestData, _jsonSerializerSettings));
                  var jsonContent = new StringContent(serializedObject, System.Text.Encoding.UTF8, "application/json");
                  HttpResponseMessage response = await client.PostAsync(new Uri(url, UriKind.Relative), jsonContent);
                  responseData = await HandleResponse(response);
  
  
                  return await Task.Run(() =&gt; JsonConvert.DeserializeObject&lt;TResult&gt;(responseData, _jsonSerializerSettings));
  
  
              }
              else
              {
  
                  throw new NullReferenceException("NullReferenceException @ PostAsync  httpclient is null WebRequest.cs");
  
              }
  
          }
  </code></pre>
  
  <p>client will use following code to execute request</p>
  
  <pre><code>new LoginService(new WebRequest()).UserLogin(userRequest);
  </code></pre>
  
  <p>inside class that implements <code>IWebRequest</code></p>
  
  <pre><code>_webRequest.PostAsync&lt;UserRequest,bool&gt;(Constants.USER_LOGIN, userRequest);
  </code></pre>
  
  <p>in <strong>second</strong> approach i am reusing the same http client object in each new request
  here , my singleton class is thread safe too.</p>
  
  <pre><code>private static readonly Lazy&lt;HttpService&gt; lazy =
          new Lazy&lt;HttpService&gt;(() =&gt; new HttpService());
  
          public static HttpService Instance { get { return lazy.Value; } }
  
  
  
          private HttpClient getConnection()
          {
  
              client = new HttpClient();
              client.Timeout = System.TimeSpan.FromMilliseconds(timeout);
  
              //client.MaxResponseContentBufferSize = 500000;
              client.BaseAddress = new Uri(baseAddress);
              return client;
          }
  </code></pre>
  
  <p>post request code </p>
  
  <pre><code>public Task&lt;HttpResponseMessage&gt; sendData(String url,String jsonData)
          {
  
              var jsonContent = new StringContent(jsonData, System.Text.Encoding.UTF8, "application/json");
  
              return getConnection().PostAsync(new Uri(url, UriKind.Relative), jsonContent);
          }
  </code></pre>
  
  <p>client will use following code to execute </p>
  
  <pre><code>HttpService.Instance.sendData(...)
  </code></pre>
  
  <p>i have gone through many libraries like <code>RestSharp</code> over web just to explore the best and i found that most of them are creating new objects per request. so i am confused which pattern fits best.</p>