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

ConnectionTimeout works fine on Linux but it does not on Windows #2070

Closed
mcNets opened this issue Jun 22, 2023 · 6 comments
Closed

ConnectionTimeout works fine on Linux but it does not on Windows #2070

mcNets opened this issue Jun 22, 2023 · 6 comments

Comments

@mcNets
Copy link

mcNets commented Jun 22, 2023

I am trying to set a ConnectionTimeout of 5 seconds and it works on Linux but it does not on Windows.

That is the code that I am using to test it:

using Microsoft.Data.SqlClient;
using System.Diagnostics;

var connectionString = new SqlConnectionStringBuilder() {
    DataSource = "10.10.10.10",
    InitialCatalog = "myCatalog",
    UserID = "userId",
    Password = "password",
    ConnectTimeout = 5
}.ConnectionString;

var watch = new Stopwatch();
var connection = new SqlConnection(connectionString);

try {
    watch.Start();
    connection.Open();
    watch.Stop();
}
catch(SqlException se) {
    watch.Stop();
    for (var x = 0; x < se.Errors.Count; x++) {
        Console.WriteLine($"Code: {se.Errors[x].Number}  {se.Errors[x].Message}");
    }
}
catch (Exception e) {
    watch.Stop();
    Console.WriteLine($"ERROR: {e.Message}");
}
finally {
    Console.WriteLine($"Time: {watch.ElapsedMilliseconds}");
    if (connection.State == System.Data.ConnectionState.Open) {
        connection.Close();
    }
    connection.Dispose();
}

On Linux I am getting this error message:

Code: 0 A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server: Could not open a connection to SQL Server)
Time: 6127

And on Windows:

Code: 10060 A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)
Time: 21489

As you can see is returning different Exception messages, and I cannot get less than 21 seconds on Windows.

I am using this package:

<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.0-preview2.23159.1" />

Operating systems:

Windows 11 Pro 22H2
Debian GNU/Linux 11 (bullseye)

@JRahnama
Copy link
Member

Can you try adding AppContext.SetSwitch("Switch.Microsoft.Data.SqlClient.UseManagedNetworkingOnWindows",true); to your main method and see the results on Windows...

Regarding the the timeout on netcore there is known issue and PR #2067 is addressing that.

@mcNets
Copy link
Author

mcNets commented Jun 22, 2023

@JRahnama it seems to work.

Do you think this will be fixed in a new version?

Code: 0 A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server: Could not open a connection to SQL Server)
Time: 5300

What is the Managed Network?

@JRahnama
Copy link
Member

well, the fact is on Windows netcore applications are using native SNI runtime files, whereas on Unix it uses managed SNI. This is the source of different error messages. Adding the context switch I provided will enable manage SNI on Windows as well.

In some other words on Windows you are using a partially different code bases than Unix or when using AppContext switch.

Our final goal would be using managed SNI every where when it is fully tested and addressed a couple of existing bugs, but I do not think this is going to happen in near future.

As a workaround you can keep using the AppContext switch to get same results on Windows and Unix.

@mcNets
Copy link
Author

mcNets commented Jun 23, 2023

Ok, thank's for the help.

@DavoudEshtehari
Copy link
Member

@mcNets Can you give it a try with the recent preview version, please?

@mcNets
Copy link
Author

mcNets commented Jul 30, 2023

@DavoudEshtehari, Now it's working.

Timeout = 5

Test SQL Server connection timeout
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server: Could not open a connection to SQL Server)

Elapsed: 5498

Timeout = 3

Test SQL Server connection timeout
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server: Could not open a connection to SQL Server)

Elapsed: 3601

I used this verion:

<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.0-preview3.23201.1" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants