locked
Correct usage of HttpClient class RRS feed

  • Question

  • Hi,

    I'm developing a Windows Store App that calls web services using HttpClient class.

    My doubt is whether to instantiate HttpClient class with a using block for each web service call or have a page instance of HttpClient initialized in OnNavigatedTo method and destroyed in OnNavigatedFrom method of the page calling the Dispose method of HttpClient. In the latter case, I also have the option of calling the method CancelPendingRequests of HttpClient to stop any pending calls when you navigate to another page.

    Typically I always prefer to dispose resources as soon as possible and then I'm tempted to instantiate each service call the HttpClient class. My doubt is linked to possible performance problems (especially on devices such as tablets) and what happens if the service call is not completed and I browse to another page (it throws an exception?).

    Sunday, April 21, 2013 5:25 AM

Answers

  • I have to doubt that you could inadvertently instantiate so many HttpClients in a Store app that you would end up causing real performance problems - I've seen thousands of threads running network connections simultaneously on Server OSes.  However, it's not a bad idea to dispose of anything you're actively not using, but you're right that there is a performance hit with spinning up something new.  This seems like one of those things that you're overthinking - just run with the dispose idea unless you have actual issues with performance in spinning up new HttpClients, and if you do, then you'll probably want to look into creating a pool of them, but I really doubt this is going to necessary.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, April 22, 2013 2:45 PM
    Moderator

All replies

  • I have to doubt that you could inadvertently instantiate so many HttpClients in a Store app that you would end up causing real performance problems - I've seen thousands of threads running network connections simultaneously on Server OSes.  However, it's not a bad idea to dispose of anything you're actively not using, but you're right that there is a performance hit with spinning up something new.  This seems like one of those things that you're overthinking - just run with the dispose idea unless you have actual issues with performance in spinning up new HttpClients, and if you do, then you'll probably want to look into creating a pool of them, but I really doubt this is going to necessary.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, April 22, 2013 2:45 PM
    Moderator
  • Hi Matt,

    if I immediately dispose http clients with "using" block, what happens if I navigate to another page and http request is not yet completed? Do I have an exception?
    Http clients initialized in OnNavigatedTo and destroyed in OnNavigatedFrom method of the page allows me to call also CancelPendingRequests but if immediately dispose http clients I cannot call CancelPendingRequests.

    Sunday, April 28, 2013 4:17 AM
  • I don't know how you would accomplish this.  If you run code on a page with http requests pending, then your http requests should be complete before you navigate away.   

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, April 29, 2013 1:35 PM
    Moderator