I am working on creating some libraries for a project at work and I wanted to make sure I have this pattern correct. Assuming that the GetWidgets method is what I am going to be exposing, which is the preferred method for this? Is there any fundamental difference in these two methods?

public async Task<List<Widget>> GetWidgets(DateTime date)
{
using (var httpClient = new HttpClient())
{
var feed = "http://example.org/feed";
var response = await httpClient.GetStringAsync(feed);
var items = await SomeOtherAsyncMethod(response);

return items.Where(item => item.StartDate.Date == date).ToList();
}
}

{
{
using (var httpClient = new HttpClient())
{
var feed = "http://example.org/feed";
var response = httpClient.GetStringAsync(feed).Result;
var items = SomeOtherAsyncMethod(response).Result;

return items.Where(item => item.StartDate.Date == date).ToList();
}
});
}


And if the second example is correct, should I be naming it GetWidgetsAsync?

I'd prefer the first method because it's making more efficient use of resources (provided the async methods you call are really async and not just wrappers around synchronous methods) and it also looks cleaner.

To quote from this MSDN blog:

... the only asynchronous methods that should be exposed are those that have scalability benefits over their synchronous counterparts ...

What you are doing in your second method is basically calling the async methods in a synchronous way just to then in turn wrap it into a task. This will have scalability issues because it's using up a dedicated thread (at least in the current implementation of the task library) while the async methods you call might have more efficient means of achieving their asynchronicity (like IO completion ports or timer callbacks)

• So there is where I think I am confused. Is var response = await httpClient.GetStringAsync(feed); var items = await SomeOtherAsyncMethod(response); not also a syncronous call? – Schandlich Oct 16 '14 at 20:38
• @Schandlich: No. await will release the control back to the calling thread and the execution of the method will continue once the async call which is being awaited has finished. The execution will either resume on the original thread or another one depending on some options you can set. See MSDN for an in-depth explanation of how it all works. – ChrisWue Oct 16 '14 at 21:40
• Thank you very much. This also helped me a lot: msdn.microsoft.com/en-us/library/hh873177(v=vs.110).aspx – Schandlich Oct 17 '14 at 0:24
httpClient.GetStringAsync(feed).Result


You shouldn't do this. Result will synchronously block for the Task to complete, negating any benefits of using async. It can also easily lead to deadlocks.

should I be naming it GetWidgetsAsync?