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
Post a Comment