title: How to dispose the data provider instance using HttpClient?
body: <p>I have created my data provider using Repository Pattern. </p>
  
  <p>First, I designed a base repository interface as following:</p>
  
  <pre><code>internal interface IGenericRepository&lt;T, in TResourceIdentifier&gt;
  {
      Task&lt;IEnumerable&lt;T&gt;&gt; GetManyAsync();
      Task&lt;T&gt; GetAsync(TResourceIdentifier id);
      Task PutAsync(T model);
      Task&lt;T&gt; PostAsync(T model);
      Task DeleteAsync(TResourceIdentifier id);
  }
  </code></pre>
  
  <p>Then I implemented it:</p>
  
  <pre><code>public class GenericRepository&lt;T, TResourceIdentifier&gt; : IDisposable, IGenericRepository&lt;T, TResourceIdentifier&gt; 
      where T : class
  {
      private bool _disposed;
      protected HttpClientHelper&lt;T, TResourceIdentifier&gt; Client;
  
      protected GenericRepository(string addressSuffix)
      {
          Client = new HttpClientHelper&lt;T, TResourceIdentifier&gt;(Properties.Settings.Url, addressSuffix);
      }
  
      public async Task&lt;IEnumerable&lt;T&gt;&gt; GetManyAsync()
      {
          return await Client.GetManyAsync();
      }
  
      // All other CRUD methods and dispose
  
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  
      private void Dispose(bool disposing)
      {
          if(_disposed || !disposing) return;
  
          if(Client != null)
          {
              var mc = Client;
              Client = null;
              mc.Dispose();
          }
  
          _disposed = true;
      }
  }
  </code></pre>
  
  <p>Then I created custom repository interface for each of my entities. For example:</p>
  
  <pre><code>internal interface IOrderRepository : IGenericRepository&lt;Order, int&gt;
  {
      Task&lt;IEnumerable&lt;Order&gt;&gt; GetOrderBySomeConditionAsync(string condition );
  
  }
  </code></pre>
  
  <p>And finally, I implemented the custom repository:</p>
  
  <pre><code>public class OrderRepository : GenericRepository&lt;Order, int&gt;, IOrderRepository
  {
      public OrderRepository(string addressSuffix) : base(addressSuffix)
      {
      }
  
      public async Task&lt;IEnumerable&lt;Order&gt;&gt; GetOrderBySomeConditionAsync(string condition)
      {
          //get all the orders (GetManyAsync()) and then returns the ones meeting the condition
      }
  }
  </code></pre>
  
  <p>Note that <code>HttpClientHelper</code>uses <code>HttpClient</code> and needs to be disposed manually. </p>
  
  <p>I have created a MVC web application and have defined the repositories at the class level as such:</p>
  
  <pre><code>IOrderRepository _orderRepository = new OrderRepository();
  </code></pre>
  
  <p>When I call <code>_orderRepository</code> in my CRUD operations, it does not hit dispose after its use. In order to fix that I have ended up implementing like this:</p>
  
  <pre><code>private async Task&lt;IEnumerable&lt;OrderViewModel&gt;&gt; GetOrders()
  {
      using(var orderRepository = new OrderRepository())
           return await orderRepository.GetManyAsync();
  }
  </code></pre>
  
  <p>This would hit the Dispose but is anti pattern. </p>
  
  <p>What am I missing in my implementation that the instance is not disposed on each call?</p>