SqlClient: A network-related or instance-specific error occurred while establishing a connection to SQL Server. On AWS Lambda

Describe the bug

With version 2.1.0 of Microsoft.Data.SqlClient attempts to connect to a SQL server database that is not hosted in AWS are met with the below exception.

After failing to find any other cause for this error I rolled back to version 2.0.1 and everything worked fine. I did not see this issue on my local (windows) machine.

Exception message: 

Microsoft.Data.SqlClient.SqlException (0x80131904): 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)

Stack trace:

 at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at Microsoft.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover, SqlAuthenticationMethod authType)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, 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)
   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()

To reproduce

Attempt to open a connection to a sql server database hosted outside aws from a lambda function.

Expected behavior

The connection should work fine, just like it does in package versions previous to this.

Further technical details

Microsoft.Data.SqlClient version: 2.1.0 .NET target: core 3.1 SQL Server version: SQL Server 2017 Operating system: AWS Lambda Runtime

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 19 (9 by maintainers)

Commits related to this issue

Most upvoted comments

@cheenamalhotra can I ask you to send artifacts link posted here one more time?

Hi @davesmits

We’re working on a few important changes also candidate for this release, it’s planned to be released as soon as we’re ready, but latest by mid-February.

Edit: Timelines are stretched to first week of March, to include a few more important fixes like issue #659 and #926

Psychic debugging attempt. Look at the tls versions that the linux machine you’re using has setup, recent docker containers on things like alpine have had known problems with not enabling the required version.

Hi @davesmits @tarasverq

Microsoft.Data.SqlClient v2.1.2 is now released that contains the fix. Closing issue as resolved.

I can confirm the same issue running version 2.1.1 in a docker container mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim. It works when downgrading to an older version.

@JRahnama

Is the issue still Microsoft.Data.SqlClient related? If yes, can you provide us a sample repro? besides that can you share your connection string properties?

Yes, It is definitely the library. There doesn’t seem to be much to the repro it fails the instant you call Open:

var con = new SqlConnection(conString);
con.Open();

The trick seems to be that it only occurs on certain platforms, and not others (Win 10 is fine, but whatever underpins the lambda runtime, Amazon Linux 2 I assume, is not).

Whatever caused this was introduced in the latest release, every prior version this same code works just fine.

The connection string looks like this:

Server=SERVERNAME.DOMAINNAME\\INSTANCENAME; Database=DB_NAME; Encrypt=yes; TrustServerCertificate=true; User Id={userId}; Password={password}