title: Calling external HTTP service using HttpClient from a Web API Action
body: <p>I am calling an external service using HttpClient from within an ASP.Net MVC 4 Web Api project running on .Net Framework 4.5</p>
  
  <p>The sample code is as follows (ignore the return values as this is sample code to test calling an external service):</p>
  
  <pre><code>public class ValuesController : ApiController
  {
      static string _address = "http://api.worldbank.org/countries?format=json";
      private string result;
  
      // GET api/values
      public IEnumerable&lt;string&gt; Get()
      {
          GetResponse();
          return new string[] { result, "value2" };
      }
  
      private async void GetResponse()
      {
          var client = new HttpClient();
          HttpResponseMessage response = await client.GetAsync(_address);
          response.EnsureSuccessStatusCode();
          result = await response.Content.ReadAsStringAsync();
      }
  }
  </code></pre>
  
  <p>While the code in the private method does indeed work the problem I have is that the Controller Get() calls the GetResponse() but it is not awaiting the result but instead immediately executes the return with result = null.</p>
  
  <p>I have also tried using a simpler synchronous call with a WebClient as follows:</p>
  
  <pre><code> // GET api/values
      public IEnumerable&lt;string&gt; Get()
      {
          //GetResponse();
  
          var client = new WebClient();
  
          result = client.DownloadString(_address);
  
          return new string[] { result, "value2" };
      }
  </code></pre>
  
  <p>which works fine.</p>
  
  <p>What am I doing wrong? Why does the Get() not await the private method completion in the async sample?</p>