Vote count:
1
I have a Web API that returns a list of people:
public async Task<HttpResponseMessage> Get()
{
var people = await _PeopleRepo.GetAll();
return Request.CreateResponse(HttpStatusCode.OK, people);
}
I have a console application that I'd like to be able to call so that it first gets the people, then iterates over them calling their ToString() method, and then completing.
I have the following method to get the people:
static async Task<List<Person>> GetAllPeople()
{
List<Person> peopleList = null;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:38263/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("People");
response.EnsureSuccessStatusCode();
if (response.IsSuccessStatusCode)
{
peopleList = await response.Content.ReadAsAsync<List<Person>>();
}
}
return peopleList;
}
I then have a second function to print the list:
static void PrintPeopleList(List<Person> people)
{
if (people == null)
{
Console.Write("No people to speak of.");
return;
}
people.ForEach(m => Console.WriteLine(m.ToString()));
}
I tried using a task factory to first download the people list with GetAllPeople() and then supply the results to PrintPeopleList() when the response is back but the compiler gives an ambiguous invocation error:
Task.Factory.StartNew(() => GetAllPeople()).ContinueWith((t) => PrintPeopleList(t.Result));
Am I way off?
asked 2 mins ago
Aucun commentaire:
Enregistrer un commentaire