c# - Using HttpClient.GetAsync to call Web API seems to hang -
i'm working on proof of concept prototype.
have asp.net c# web form (visual studio 2013, .net 4.5) page. on button click this:
list<blog> blogs = null; protected void btnloaddata_click(object sender, eventargs e) { //...; switch (int.parse(rbldatasource.selectedvalue)) { //...; case 4: runasyncblogs().wait(); break; default: blogs = localgetter.getblogs(); break; } //...; }
runasyncblogs looks this:
static async task runasyncblogs() { using (var client = new httpclient()) { client.baseaddress = new uri("http://localhost/wapidemo/"); client.defaultrequestheaders.accept.clear(); client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json")); // http httpresponsemessage response = await client.getasync("api/blogs"); if (response.issuccessstatuscode) { list<blog> thisbloglist = await response.content.readasasync<list<blog>>(); var cnt = thisbloglist.count(); } } }
the code stops @ response = await client.getasync call. when stops, debugger acts request has ended, browser still waiting.
if run console app (i copy , paste runasync() method it) calls web api same way, data. so, believe web api app responding expected.
this:
runasyncblogs().wait();
is deadlocking code. synchronously blocking async method call trying marshal continuation on implicitly captured synchronization context. why shouldn't block on async code
protected async void btnloaddata_click(object sender, eventargs e) { // other stuff await runasyncblogs(); }
Comments
Post a Comment