Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG][csharp-netcore] C# Client for Net Core generate port exaustion #9060

Closed
lucamazzanti opened this issue Mar 24, 2021 · 6 comments · Fixed by #9085 or #9109
Closed

[BUG][csharp-netcore] C# Client for Net Core generate port exaustion #9060

lucamazzanti opened this issue Mar 24, 2021 · 6 comments · Fixed by #9085 or #9109

Comments

@lucamazzanti
Copy link
Contributor

lucamazzanti commented Mar 24, 2021

Description

I moved from Net Framework 4.7 to Net Core 3.1 and 5 and I have a critical issue.

The csharp-netcore client uses RestSharp and is not stable with Net Core actually. See this issue.
It seems to be a net core breaking change they won't patch in time and actually this is impacting on RestSharp.

It silently open at every call a new socket, creating a new HttpWebRequest each time.
This is a behavior totally different in NetFramework where it keep the same.
You can reproduce it easily generating a simple client and using it in both verisons.
Test multiple calls and watch the connection in Wireshark, I can further add images or a report if you prefer.

While NetFramework honors the keep-alive header (supported also by the server) in Net Core it doesnt.

  • In NetFramework it do the handshake and TLS connection just 1 time, and it consumes just one socket keeping working on it with the next calls.
  • In Net Core not, it open a new socket each times.

This is critical because you cannot understand it when moving your client to net core until you have a feedback from the server closing the connections to all.

I see there is a version with HttpClient, i tried it but it create every time:
var handler = new HttpClientHandler(); var client = new HttpClient();

openapi-generator version

openapi-generator-cli-5.1.0.jar with csharp-netcore client without settings.

@wing328
Copy link
Member

wing328 commented Mar 24, 2021

The latest version of OpenAPI Generator v5.1.0 supports HttpClient (--library httpclient). Please give it a try.

@lucamazzanti
Copy link
Contributor Author

I see there is a version with HttpClient, i tried it but it create every time:

var handler = new HttpClientHandler(); 
var client = new HttpClient();

So the issue remains

@lucamazzanti lucamazzanti changed the title [BUG] C# Client for Net Core generate port exaustion [BUG][csharp-netcore] C# Client for Net Core generate port exaustion Mar 24, 2021
@devhl-labs
Copy link
Contributor

You'll want this pr once it's merged. You pass in the HttpClient, so you control it entirely.

#9073

Note that it requires a bit of post processing, but here is a script for it, just edit the parameters to suite your needs.

java -jar "../openapi-generator/modules/openapi-generator-cli/target/openapi-generator-cli.jar" generate `
    -g csharp-netcore `
    -i "your-swagger-file.yml" `
    -c generator-config.json `
    -o output `
    -t templates `
    --library httpclient

$files = Get-ChildItem output -Recurse | Where-Object {-Not($_.PSIsContainer)}
foreach ($file in $files)
{
    $content=Get-Content $file.PSPath

    if (-Not($content)){
        continue;
    }

    # if null reference types are enabled
    $content=$content -replace '\?{3,4}', '?' # replace every three to four consecutive occurrences of '?' with a single one

    # if null reference types are not enabled
    # $content=$content.Replace("????", "?") # reference type
    # $content=$content.Replace("???", "") # value type

    Set-Content $file.PSPath $content    
}

@Blackclaws
Copy link
Contributor

You are absolutely right. The issue is that HttpClient is not disposed. Will fix immediately. There is also the additional property reUseHttpClient which should alleviate the problem as only a single HttpClient is used for the whole ApiClient then.

Passing in an HttpClient is on the roadmap.

@Blackclaws
Copy link
Contributor

After some further investigation the issue appears a bit larger as even a static HttpClient will break after some usage. I'll investigate and try to get a pull request up today.

@Blackclaws
Copy link
Contributor

Pull request is up which fixes this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment