SqlClient: Microsoft.Data.SQLClient throws exception trying to access Azure SQL Database
I am running two .NET6 apps on Linux Azure Web App containers, which need connections to a Azure SQL database. The first one connects to the DB without problem, the second one, deployed on the same service plan with the same configuration, throws the following generic exception:
Unhandled exception. Microsoft.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the login process. (provider: TCP Provider, error: 35 - An internal exception was caught)
---> System.IO.IOException: Unable to write data to the transport connection: Connection reset by peer.
---> System.Net.Sockets.SocketException (104): Connection reset by peer
at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 count)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.Stream.Write(ReadOnlySpan`1 buffer)
at System.Net.Sockets.NetworkStream.Write(ReadOnlySpan`1 buffer)
at Microsoft.Data.SqlClient.SNI.SslOverTdsStream.Write(ReadOnlySpan`1 buffer)
at Microsoft.Data.SqlClient.SNI.SslOverTdsStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Security.SslStream.WriteSingleChunk[TIOAdapter](TIOAdapter writeAdapter, ReadOnlyMemory`1 buffer)
at System.Net.Security.SslStream.WriteAsyncInternal[TIOAdapter](TIOAdapter writeAdapter, ReadOnlyMemory`1 buffer)
at System.Net.Security.SslStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at Microsoft.Data.SqlClient.SNI.SNIPacket.WriteToStream(Stream stream)
at Microsoft.Data.SqlClient.SNI.SNITCPHandle.Send(SNIPacket packet)
at Microsoft.Data.ProviderBase.DbConnectionPool.CheckPoolBlockingPeriod(Exception e)
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()
at BBB.Identity.Configuration.IdentityServerConfigurationProvider.Load() in D:\a\1\s\BBB.Identity\Configuration\IdentityServerConfigurationProvider.cs:line 26
at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
at BBB.Identity.Configuration.IdentityServerConfigurationManager.SetIdentityConfigurationRoot() in D:\a\1\s\BBB.Identity\Configuration\IdentityServerConfigurationManager.cs:line 34
at BBB.Identity.Configuration.IdentityServerConfigurationManager.InitializeIdentityServerConfiguration() in D:\a\1\s\BBB.Identity\Configuration\IdentityServerConfigurationManager.cs:line 17
at Program.<Main>$(String[] args) in D:\a\1\s\BBB.Identity\Program.cs:line 15
To reproduce
Not sure how to provide reproducibility as it is a private project. I myself can’t reproduce the issue somewhere else: When deploying the app locally and connecting it to the Azure SQL db it works.
Expected behavior
A working connection to the Azure SQL database.
Further technical details
Microsoft.Data.SqlClient version: 4.1.0 .NET target: .NET6(.0.301) SQL Server version: Azure SQL database Operating system: Azure App Service Plan Linux
Both apps are on the same service plan, have the same connection string, the same configuration and the same software versions. I can not imagine where the difference lies, which could produce the connection error.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 2
- Comments: 16 (4 by maintainers)
None of any value. The infrastructure engineer I work with has an Azure support case open but it hasn’t produced anything useful yet. I will be sure to update this post as we go along/result of the support case.
Same started to appear randomly after migrating to docker containers hosted in AKS on Linux machine, but with no change to .NET version (6.0), min TLS version on SQL Server set to 1.2.
System.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: SSL Provider, error: 31 - Encryption(ssl/tls) handshake failed) —> System.AggregateException: One or more errors occurred. (Unable to write data to the transport connection: Connection reset by peer.)
So glad I am not the only one, proves I am not crazy 😉
The Azure Support was really trying once I got beyond the stages where they just recommend all the stuff from the docs which you mentioned already in the initial ticket as not working, but ultimately wasn’t able to help.
Workaround was to just use the Windows based App Service containers and move on…