12
\$\begingroup\$

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();
    }
}

public Task<List<Widget>> GetWidgets(DateTime date)
{
    return Task.Factory.StartNew(() =>
    {
        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?

\$\endgroup\$
6
\$\begingroup\$

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)

\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – Schandlich Oct 16 '14 at 20:38
  • \$\begingroup\$ @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. \$\endgroup\$ – ChrisWue Oct 16 '14 at 21:40
  • \$\begingroup\$ Thank you very much. This also helped me a lot: msdn.microsoft.com/en-us/library/hh873177(v=vs.110).aspx \$\endgroup\$ – Schandlich Oct 17 '14 at 0:24
0
\$\begingroup\$
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?

Yes, both your versions should be named that way, to follow the Task-based Asynchronous Pattern.

\$\endgroup\$

protected by Jamal Oct 7 '18 at 7:02

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.