4

I have this very simple WebApi method:

[HttpGet]
public IHttpActionResult Foo()
{
    Thread.Sleep(3000);
    return Ok("Bar");
}

And I have these two methods in a console application that call it:

async Task UsingWebClient()
{
    Task<string> task = new WebClient().DownloadStringTaskAsync (new Uri ("http://localhost.fiddler:63710/api/producttype/Foo"));
    Console.WriteLine("WebClient - Before calling wait");
    string result = await task;
    Console.WriteLine("WebClient - After calling wait");
}

async Task UsingHttpClient()
{
    Task<string> task = new HttpClient().GetStringAsync (new Uri ("http://localhost.fiddler:63710/api/producttype/Foo"));
    Console.WriteLine("HttpClient - Before calling wait");
    string result = await task;
    Console.WriteLine("HttpClient - After calling wait");
}

And I am calling these methods from LinqPad like this:

async Task Main()
{
    await UsingWebClient();
    await UsingHttpClient();
}

I was monitoring the traffic using Fiddler and I noticed that:

  • when using WebClient the request to the web api is made immediately and then execution continues to Console.WriteLine("WebClient - Before calling wait");
  • when using HttpClient the request to the web api is not made until the call to await task;

I'm trying to understand why the request is not made immediately when using HttpClient. Can anyone point me in the right direction?

This is not a duplicate question. I'm not looking for reasons to choose one option over the other - I'll use HttpClient. I would like to know specifically why the request is created at a later stage when using HttpClient.

Thanks, David

dlarkin77
  • 867
  • 1
  • 11
  • 27
  • possible duplicate of [Need help deciding between HttpClient and WebClient](http://stackoverflow.com/questions/20530152/need-help-deciding-between-httpclient-and-webclient) – Leandro Bardelli Apr 29 '15 at 15:18
  • 2
    HttpClient is newer and probably optimized better for supporting Async Requests. – RickJames Apr 29 '15 at 15:35
  • just to be sure: how often did you test this and are your really sure that you can really decide what happened when? – Random Dev Apr 29 '15 at 15:37
  • @CarstenKönig I've run those methods quite a few times today and what I've been seeing in Fiddler is pretty predicatable. If I set a breakpoint in the second line of the UsingWebClient method I can clearly see that the request has been sent in Fiddler. If I set a breakpoint in the third line of UsingHttpClient I can see that the request has not been sent and it is only sent after the third line executes. – dlarkin77 Apr 29 '15 at 15:55
  • CAn you show the code calling `UsingWebClient` and `UsingHttpClient`? – Paulo Morgado Apr 29 '15 at 22:36
  • @PauloMorgado I've edited my question to show the calling code – dlarkin77 Apr 30 '15 at 12:10
  • 1
    If you call `UsingHttpClient` first and then `UsingWebClient`, do those methods behave the same way? Or do you notice that they exchanged behavior? – Paulo Morgado Apr 30 '15 at 15:37
  • Calling UsingHttpClient first and then UsingWebClient does not result in any changed behavior. – dlarkin77 May 01 '15 at 08:50

1 Answers1

1

Since both of the requests are async, none of them should delay execution of your current thread (significantly).

It is possible, though, that one of them can send the request before the current thread reaches the next line, while the other cannot.

These kinds of timing issues can happen in asynchronous/parallel environments and they're nothing to worry about as long as you don't separate logically successive operations.

  • But doesn't the fact that the code using WebClient sends the request to the web api immediately imply that it will get a response quicker and give better performance? – dlarkin77 Apr 30 '15 at 12:13
  • It may imply that, but it is not neccessarily so. The time they take to initialize might be different, among a bunch of other things as well. If you want to measure their performance, then do so - the average time between sending the request and getting a response is not very difficult to measure. – Sándor Mátyás Márton Apr 30 '15 at 13:21