Calling external HTTP service using HttpClient from a Web API Action

51,646

Aha, I needed to do the following (return a Task rather then void):

 // GET api/values
    public async Task<IEnumerable<string>> Get()
    {
        var result = await GetExternalResponse();

        return new string[] { result, "value2" };
    }

    private async Task<string> GetExternalResponse()
    {
        var client = new HttpClient();
        HttpResponseMessage response = await client.GetAsync(_address);
        response.EnsureSuccessStatusCode();
        var result = await response.Content.ReadAsStringAsync();
        return result;
    }

Also I hadn't realised I could mark the Get() operation as async which is what allowed me to await the external call.

Thanks to Stephen Cleary for his blog post Async and Await which pointed me in the right direction.

Share:
51,646
Redeemed1
Author by

Redeemed1

Many many years of internet-delivered Enterprise Systems using MS Technologies for Cloud and Web. Christian believer redeemed by Jesus. Guitarist, Song Writer and Music Producer with Nuendo

Updated on December 30, 2020

Comments

  • Redeemed1
    Redeemed1 over 3 years

    I am calling an external service using HttpClient from within an ASP.Net MVC 4 Web Api project running on .Net Framework 4.5

    The sample code is as follows (ignore the return values as this is sample code to test calling an external service):

    public class ValuesController : ApiController
    {
        static string _address = "http://api.worldbank.org/countries?format=json";
        private string result;
    
        // GET api/values
        public IEnumerable<string> 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();
        }
    }
    

    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.

    I have also tried using a simpler synchronous call with a WebClient as follows:

     // GET api/values
        public IEnumerable<string> Get()
        {
            //GetResponse();
    
            var client = new WebClient();
    
            result = client.DownloadString(_address);
    
            return new string[] { result, "value2" };
        }
    

    which works fine.

    What am I doing wrong? Why does the Get() not await the private method completion in the async sample?

  • Subrata Sarkar
    Subrata Sarkar over 8 years
    Just what I was looking for since last 3 days! Simple once you know it :) The shortest and the most effective answer.
  • Redeemed1
    Redeemed1 over 6 years
    Karan, thank you for your effort here but this is not related to the question. Your response relates to Authentication, the question relates to Async Tasks around a non-authenticated api