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

SqlException "provider: TCP Provider, error: 26 - Error Locating Server/Instance Specified" in 5.2.0 on Mac, not Windows #2477

Closed
marshall-lerner opened this issue May 2, 2024 · 4 comments
Labels
2️⃣ Duplicate Issue/PR that is a duplicate and already exists.

Comments

@marshall-lerner
Copy link

Describe the bug

Hey all,

We are encountering an error with SqlClient version 5.2.0 when trying to connect to a sql connection ONLY ON MAC using SqlClient 5.2.0, but not when using 5.1.5, using a simple Console App with the below code running. Note that running this on Windows works fine, but when running it on a Mac, it does not.

Exception message: Microsoft.Data.SqlClient.SqlException: "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: 26 - Error Locating Server/Instance Specified)"
Stack trace:
System.Net.Sockets.SocketException: "Undefined error: 0"
  at at Microsoft.Data.SqlClient.SNI.SSRP.GetPortByInstanceName(String browserHostName, String instanceName, TimeoutTimer timeout, Boolean allIPsInParallel, SqlConnectionIPAddressPreference ipPreference)
   at Microsoft.Data.SqlClient.SNI.SNIProxy.CreateTcpHandle(DataSource details, TimeoutTimer timeout, Boolean parallel, SqlConnectionIPAddressPreference ipPreference, String cachedFQDN, SQLDNSInfo& pendingDNSInfo, Boolean tlsFirst, String hostNameInCertificate, String serverCertificateFilename)
  --- End of inner exception stack trace ---
  at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\n   at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, SqlCommand command, Boolean callerHasConnectionLock, Boolean asyncClose)
   at Microsoft.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, TimeoutTimer timeout, SqlConnectionString connectionOptions, Boolean withFailover)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, TimeoutTimer timeout, Boolean withFailover)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken, DbConnectionPool pool, Func`3 accessTokenCallback)
   at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides)
   at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides)
   at Microsoft.Data.SqlClient.SqlConnection.Open()\n   at Program.<<Main>$>d__0.MoveNext() in /src/testsqlconnection/testsqlconnection/Program.cs:28

To reproduce

Include a complete code listing (or project/solution) that we can run to reproduce the issue.

using Microsoft.Data.SqlClient;

string connectionString = "Data Source=<Connection String>;Encrypt=True";
string accessToken = "<AccessToken>";

SqlConnectionStringBuilder sqlConnectionStringBuilder = null;
try
{
    sqlConnectionStringBuilder = new SqlConnectionStringBuilder(connectionString);
}
catch (Exception ex)
{
    sqlConnectionStringBuilder = new SqlConnectionStringBuilder() { DataSource = connectionString };
}

sqlConnectionStringBuilder.Encrypt = true;

var sqlconnection = new SqlConnection(sqlConnectionStringBuilder.ToString());
sqlconnection.AccessToken = accessToken;

string query = $"SELECT * FROM (VALUES ('John Smith',  31) , ('Kayla Jones', 33)) AS Employee(EmpName, DepID);";

using SqlCommand command = new SqlCommand(query, sqlconnection);

sqlconnection.Open(); // THIS IS THE LINE WHERE THE EXCEPTION OCCURS

using var reader = await command.ExecuteReaderAsync();

List<Object> results = new List<Object>();
while (reader.Read())
{
    results.Add(
        new
        {
            EmpName = reader["EmpName"],
            DepID = reader["DepID"]
        });
}

Console.WriteLine(results);

Further technical details

Microsoft.Data.SqlClient version: 5.2.0
.NET target: net8.0, net7.0
SQL Server version: (e.g. SQL Server 2017) I'm not sure...
Operating system: macOS 14.4.1

Additional context
Add any other context about the problem here.

@ErikEJ
Copy link
Contributor

ErikEJ commented May 2, 2024

Please share your connection string ( anonymized)

@DavoudEshtehari DavoudEshtehari added the 🆕 Triage Needed For new issues, not triaged yet. label May 3, 2024
@DavoudEshtehari
Copy link
Contributor

This could be a duplicate of #2385. Could you verify it with the pipeline artifacts, which it has the fix?

@marshall-lerner
Copy link
Author

marshall-lerner commented May 3, 2024

This could be a duplicate of #2385. Could you verify it with the pipeline artifacts, which it has the fix?

Yes, this pipeline artifact does not encounter the same error, so it looks like it has fixed the issue!

@kf-gonzalez2 kf-gonzalez2 added 2️⃣ Duplicate Issue/PR that is a duplicate and already exists. and removed 🆕 Triage Needed For new issues, not triaged yet. labels May 7, 2024
@JRahnama
Copy link
Contributor

JRahnama commented May 8, 2024

Closing the issue as a duplicate of #2385

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2️⃣ Duplicate Issue/PR that is a duplicate and already exists.
Projects
Development

No branches or pull requests

5 participants