c# - WebClient vs. HttpClient - Async request -
i have simple webapi method:
[httpget] public ihttpactionresult foo() { thread.sleep(3000); return ok("bar"); }
and have these 2 methods in console application 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 calling these methods linqpad this:
async task main() { await usingwebclient(); await usinghttpclient(); }
i monitoring traffic using fiddler , noticed that:
- when using webclient request web api made , execution continues console.writeline("webclient - before calling wait");
- when using httpclient request web api not made until call await task;
i'm trying understand why request not made when using httpclient. can point me in right direction?
this not duplicate question. i'm not looking reasons choose 1 option on other - i'll use httpclient. know why request created @ later stage when using httpclient.
thanks, david
since both of requests async, none of them should delay execution of current thread (significantly).
it possible, though, 1 of them can send request before current thread reaches next line, while other cannot.
these kinds of timing issues can happen in asynchronous/parallel environments , they're nothing worry long don't separate logically successive operations.
Comments
Post a Comment