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

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -