locked
HttpClient static instance

    Question

  • hi, I was wondering how to use httpClient as a singleton class. what I want is to create a static class which is private and be able to set the URL from methods that's makes calls to the httpclient methods... and then receive a Json response back from that class. I want to be able to deserialize the Json object from different classes since I will be making calls to multiple APIs. how do I go about this approach? what's the best practice? since the async methods are void can I really get the response string?
    • Edited by BENJIMOLA Tuesday, February 17, 2015 1:49 PM
    Tuesday, February 17, 2015 1:48 PM

Answers

  • Why exactlly do you want to use a HttpClient Singleton to make calls to different APIs instead of simply using separate HttpClients in the sections you need them?

    Async methods need to either return void or a Task object. The later approach is used by the HttpClient's GetAsync methods. So basically when you call the method it starts the call and the Task object allows you to be notified when it has been completed. When using an await in a method that method basically gets broken up around the await - everything before it will execute normally and everything after it will be done in the callback (where you have the result of the async method through the Task object). So if you'd wrap a HttpClient's call you would have to return the Task object and any calling method would have to await it.

    async/await is making this callback logic a lot easier to write/read than regular callbacks but it still works the same way.

    Tuesday, February 17, 2015 2:16 PM
  • bad idea. instances members are not alwways thread safe. so maaking it singleton can be a problem

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, February 23, 2015 9:22 AM

All replies

  • Why exactlly do you want to use a HttpClient Singleton to make calls to different APIs instead of simply using separate HttpClients in the sections you need them?

    Async methods need to either return void or a Task object. The later approach is used by the HttpClient's GetAsync methods. So basically when you call the method it starts the call and the Task object allows you to be notified when it has been completed. When using an await in a method that method basically gets broken up around the await - everything before it will execute normally and everything after it will be done in the callback (where you have the result of the async method through the Task object). So if you'd wrap a HttpClient's call you would have to return the Task object and any calling method would have to await it.

    async/await is making this callback logic a lot easier to write/read than regular callbacks but it still works the same way.

    Tuesday, February 17, 2015 2:16 PM
  • I just felt like having one class that handles all connection calls and send a Json string instead of having multiple httpclients... I just felt it will be easier to pass in string from different buttons and get a response which is received by same method that pass the URL string then do the loop from them methods. 
    • Edited by BENJIMOLA Tuesday, February 17, 2015 2:31 PM
    Tuesday, February 17, 2015 2:27 PM
  • It’s not important whether you use one singleton class to connect server. You can create a wrapper, public some methods for your purpose and make the request seem to be simple.

    Monday, February 23, 2015 8:08 AM
  • bad idea. instances members are not alwways thread safe. so maaking it singleton can be a problem

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, February 23, 2015 9:22 AM