Skip to main content
Question Unprotected by Toby Speight
edited title
Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257

Get values from Web service asyncronouslyasynchronously

Tweeted twitter.com/StackCodeReview/status/1571106708436418562
Move description of mechanism into body, and write purpose in title
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327

Async/await and Task vs Task.Factory.StartNew and Result Get values from Web service asyncronously

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

The first uses async/await and Task:

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

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

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

Async/await and Task vs Task.Factory.StartNew and Result

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?

Get values from Web service asyncronously

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

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

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

Question Protected by Jamal
edited tags
Link
ChrisWue
  • 20.6k
  • 4
  • 43
  • 107
added 4 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Source Link
Schandlich
  • 223
  • 1
  • 2
  • 5
Loading