title: Avoid returning type Task<Object> from an async function when using HttpClient
body: <p>I am making HTTP calls using the <code>System.Net.Http.HttpClient</code>. It seems that all calls must be asynchronous.</p>
  
  <p>Let's say I have a project structure of the following:
  MVC Web App -> Business Layer -> Data Layer</p>
  
  <p>In the Data Layer I am making HTTP calls to a Web API to return data and I end up using a function like this:</p>
  
  <pre><code>public async Task&lt;IList&lt;Product&gt;&gt; GetProducts()
  {
       HttpResponseMessage response = await client.GetAsync("api/products");
       string data = await response.Content.ReadAsStringAsync();
       IList&lt;Product&gt; products = JsonConvert.DeserializeObject&lt;IList&lt;Product&gt;&gt;(data);
  
       return products;
  }
  </code></pre>
  
  <p>this then goes through to the BusinessLayer:</p>
  
  <pre><code>public Task&lt;IList&lt;Product&gt;&gt; GetProducts(string name = null)
  {
       return _repository.GetProducts(name);
  }
  </code></pre>
  
  <p>Then finally in the MVC controller:</p>
  
  <pre><code>public IActionResult Index()
  {
      Task&lt;IList&lt;Product&gt;&gt; ProductsTask = _manager.GetProducts();
  
      IList&lt;Product&gt; ProductsNonTask = products.Result.ToList();
  
      return View();
  }
  </code></pre>
  
  <p>Do I really have to make every single function return a list of type <code>Task&lt;IList&lt;Product&gt;&gt;</code> leading up to my MVC controller? As you can see in the MVC controller I have to retrieve the products initially with Task wrapped around them. The debugger looks kind of strange when I go to view the list of products through it. So then as you can see I convert them to a regular list of product.</p>
  
  <p>I am wondering if it is the correct thing to do to have all of my functions return type <code>Task&lt;IList&lt;Product&gt;&gt;</code> all the way up to my MVC controller or if there is a workaround so that my functions can still return the a standard list of product but continue to use the async capabilities of the <code>HttpClient</code>?</p>
  
  <p><strong>UPDATE: Is there anything wrong with doing the following:</strong></p>
  
  <pre><code>public IList&lt;Product&gt; GetProducts()
  {
       Task&lt;HttpResponseMessage&gt; response = client.GetAsync("api/products");
       if (response.Result.IsSuccessStatusCode)
       {
            string data = response.Result.Content.ReadAsStringAsync().Result;
            IList&lt;Product&gt; products = JsonConvert.DeserializeObject&lt;IList&lt;Product&gt;&gt;(data);
  
            retVal = products;
       }
  }
  </code></pre>