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

8
  • possible duplicate of Need help deciding between HttpClient and WebClient Commented Apr 29, 2015 at 15:18
  • 2
    HttpClient is newer and probably optimized better for supporting Async Requests. Commented Apr 29, 2015 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? Commented Apr 29, 2015 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. Commented Apr 29, 2015 at 15:55
  • 1
    If you call UsingHttpClient first and then UsingWebClient, do those methods behave the same way? Or do you notice that they exchanged behavior? Commented Apr 30, 2015 at 15:37

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.