Microsoft.Data.SQLClient throws exception trying to access Azure SQL Database

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
tpickard84commented, Sep 1, 2022

@tpickard84 and @sh4d0v1 any update on the issue with Azure support? Is the issue resolved?

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.

1reaction
oresttkachukdcommented, Feb 21, 2023

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.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Microsoft.Data.SqlClinet error connecting to Azure SQL ...
I have a client application that connects to Azure SQL Database ... and the app now throws an error when trying to connect...
Read more >
Troubleshoot common connection issues to Azure SQL ...
Provides steps to troubleshoot Azure SQL Database connection issues ... Error 26: Error Locating server specified. System.Data.SqlClient.
Read more >
Azure SQL Database connection exception (System.Data. ...
I just want to use the SQL database hosted on Azure in my project. System.Data.SqlClient.SqlException (0x80131904): Login failed for user ' ...
Read more >
How to resolve "Microsoft.Data.SqlClient is not supported on ...
This article explains how to fix "Microsoft.Data.SqlClient is not supported on this platform" error in an Azure Function App.
Read more >
Azure SQL Connection - Radzen Blazor Studio
Hello, I am trying to access an Azure SQL database. ... Studio and I get this error: Login failed for user 'SchB_Admin'. at...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found