|
-
Jan 12th, 2024, 09:48 AM
#1
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.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jan 12th, 2024, 09:54 AM
#2
Re: How to call httpClient with using
What is the error you're getting? The first C# example using var should work just fine.
-
Jan 12th, 2024, 09:58 AM
#3
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.
 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
-
Jan 12th, 2024, 09:58 AM
#4
Re: How to call httpClient with using
 Originally Posted by sapator
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.
-
Jan 12th, 2024, 10:02 AM
#5
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 }
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jan 12th, 2024, 12:27 PM
#6
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|