c# - Do I need to wait for asynchronous method to complete before disposing HttpClient? -



c# - Do I need to wait for asynchronous method to complete before disposing HttpClient? -

i wonder if code works expected (send string web application):

using (httpclient httpclient = util.createhttpclient()) { httpclient.postasjsonasync("theurl", somestr); }

since postasjsonasync doesn't finish execution immediately, , httpclient disposed when exiting block, request sent properly?

or have wait task this:

using (httpclient httpclient = util.createhttpclient()) { httpclient.postasjsonasync("theurl", somestr).wait(); }

when using asynchronous api of httpclient, recommended await these methods:

using (httpclient httpclient = util.createhttpclient()) { await httpclient.postasjsonasync("theurl", somestr); }

that way, ensure completion of asynchronous method , create sure httpclient isn't disposed until request sent.

if need synchronous api, consider looking @ webclient.

c# .net async-await dotnet-httpclient

Comments

Popular posts from this blog

maven fortify plugin : Unable to load build session with ID XXXXX .. See log file for more details -

c# - Primavera WebServices does not return any data -

android - Display emoji panel with genymotion - keyboard/touch input? -