c# - Simple HttpClient test failing on Mono -
when executing simple little test on mono (3.2.1) on mac os x never prints response console instead says shutting down finalizer thread timed out.
there wrong code or mono misbehaving?
using system; using system.net.http; namespace vendtest { class mainclass { public static void main(string[] args) { client client = new client(); client.httpclientcall(); } } public class client { httpclient client; public client() { client = new httpclient(); } public async void httpclientcall() { httpclient httpclient = new httpclient(); httpresponsemessage response = await httpclient.getasync("http://vendhq.com"); string responseasstring = await response.content.readasstringasync(); console.writeline(responseasstring); } } }
you should never use async void methods , 1 reason why. main() end before httpclientcall() completes. , since exiting main() terminates whole application, nothing printed.
what should change method async task , wait() in main(). (mixing await , wait() can lead deadlocks, it's right solution for console applications.)
class mainclass { public static void main() { new client().httpclientcallasync().wait(); } } public class client { httpclient client = new httpclient(); public async task httpclientcallasync() { httpclient httpclient = new httpclient(); httpresponsemessage response = await httpclient.getasync("http://vendhq.com"); string responseasstring = await response.content.readasstringasync(); console.writeline(responseasstring); } }
Comments
Post a Comment