HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Get values from Web service asynchronously

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
getserviceasynchronouslywebvaluesfrom

Problem

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 between these two methods?

The first uses async/await and Task:

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


The alternative uses Task.Factory.StartNew() and Result:

public Task> 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 version is correct, should I be naming it GetWidgetsAsync()?

Solution

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)

Context

StackExchange Code Review Q#66914, answer score: 7

Revisions (0)

No revisions yet.