Results 1 to 6 of 6

Thread: How to call httpClient with using

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    How to call httpClient with using

    I'm translating a console vb app and I'm stuck here:

    Code:
    Using client As New HttpClient()
    
    end using
    I'm trying every combination and it does not seem to compile:
    Code:
    using (var client = new HttpClient())
    {
    }
    
    using (client = new HttpClient())
    {
    }
    
     using  var client = new HttpClient();
    This will work but does not implement using:
    Code:
     HttpClient client = new HttpClient();

    So how does this compile?
    Thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,398

    Re: How to call httpClient with using

    What is the error you're getting? The first C# example using var should work just fine.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How to call httpClient with using

    Yeah, I jsut implemented some stuff in C# using HttpClient ... while searching how to use it (I'm translating a Java library to C#) ... I found a comment in the docs that HttpClient is intended to be a single use object - ie, you create a single version for you app... it's not supposed to be created/dropped per call ...
    https://learn.microsoft.com/en-us/do...ent-guidelines

    It uses connection pooling, so creating multiple HttpClient objects can deplete the resources quickly.
    Quote Originally Posted by msdn
    Use a static or singleton HttpClient instance with PooledConnectionLifetime set to the desired interval, such as 2 minutes, depending on expected DNS changes. This solves both the port exhaustion and DNS changes problems without adding the overhead of IHttpClientFactory. If you need to be able to mock your handler, you can register it separately.
    If you only use a limited number of HttpClient instances, that's also an acceptable strategy. What matters is that they're not created and disposed with each request, as they each contain a connection pool. Using more than one instance is necessary for scenarios with multiple proxies or to separate cookie containers without completely disabling cookie handling.
    I thought I had found an example that uses the using keyword, but it was the response that it was using, not the client itself.

    So it seems that the short answer is that you cannot use a using block with HttpClient. How/why it works in VB is beyond me.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,958

    Re: How to call httpClient with using

    Quote Originally Posted by sapator View Post
    I'm translating a console vb app and I'm stuck here:

    Code:
    Using client As New HttpClient()
    
    end using
    I'm trying every combination and it does not seem to compile:
    Code:
    using (var client = new HttpClient())
    {
    }
    
    using (client = new HttpClient())
    {
    }
    
     using  var client = new HttpClient();
    This will work but does not implement using:
    Code:
     HttpClient client = new HttpClient();

    So how does this compile?
    Thanks.
    The first one looks like it should compile without errors. More fundamentally though it isn't considered best practice to wrap calls to HttpClient in a using block. If you look at https://learn.microsoft.com/en-us/do...ttp.httpclient you can see it flags this and links to https://learn.microsoft.com/en-us/do...ttp-httpclient with a full explanation.

  5. #5

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: How to call httpClient with using

    Hello.
    Please disregard and sorry for the inconvenience.
    Man I hate c# . I was putting the using outside of the class last }
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How to call httpClient with using

    hahaha.... yeah, that's not going to work. Welcome to that club... I've also misjudged what bracket I'm in more than once and dumped an entire method into the middle of a loop... oops. As well as pulling things outside of a method completely...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width