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?
Aha, I needed to do the following (return a Task rather then void):
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.
With the username and password call Httpclient. In case of API required authentication.