1

This is not a duplicate of A connection was successfully established with the server, but then an error occurred during the pre-login handshake . I have two applications, one built on .NET Framework 4.8, and a new one on .NET 6. Both are hosted on the same application server running Windows 2019 which connects to a SQL Server 2019 running on Windows 2019.

The old application never has any problem connecting to the database server, but the new application built using .NET 6 with Microsoft.Data.SqlServer throws this error while opening connection to the database:

Connection Timeout Expired. The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement. This could be because the pre-login handshake failed or the server was unable to respond back in time. The duration spent while attempting to connect to this server was - [Pre-Login] initialization=21001; handshake=0;

An important observation is that error comes most of times but not always. The application runs fines for few hours but once it starts receiving connection error, the error persists for hours and then resolves without any restart etc. Application does not have anything special except that it opens few hundred TCP connections to other Computers on the network and closes in 5-10 minutes. The issue was not observed on several other implementations on other clients.

Apparently it seems that there is some misconfiguration for SSL certificate but as per DBA there is no SSL configured. I am using encrypt=no; to specify it and have tried TrustServerCertificate=yes; too. The connection works fine from SSMS with or without selecting "Encrypt Connection". A third application running on same sever built on .NET 4.8 also does not face this issue.

Have searched for solutions on internet including A connection was successfully established with the server, but then an error occurred during the pre-login handshake. Have tried building my application with Microsoft.Data.SqlServer [6.0.5 and 6.0.11 and 7.x] and System.Data.SqlServer. Have tried restarting IIS, and Windows on application server (cannot restart database server). Tried swapping IP or DB server name for connection string.

I am expecting that some SQL Server or my application configuration / environment is causing this issue.

Zubaer Naseem
  • 454
  • 7
  • 14
  • 1
    Aside... _but as per DBA there is no SSL configured._ There is always SSL configured, even if it's only the default self-signed certificate that's created and configured by SQL Server's setup.exe. What your DBA needs to do is check in SQL Server Configuration Manager whether or not "force encryption" is enabled in the properties of the instance's Protocols node - that would require an encrypted connection even if the client's connection string says otherwise. But if all of your other clients are connecting OK without encryption (including SSMS) then this is probably not the issue. – AlwaysLearning Apr 01 '23 at 12:15
  • Worth pointing out: since Microsoft.Data.SqlClient 5.0 the `Encrypt` connection string property changed from Boolean, true/false, to [SqlConnectionEncryptOption](https://learn.microsoft.com/en-us/dotnet/api/microsoft.data.sqlclient.sqlconnectionencryptoption), Optional/Mandatory/Strict. – AlwaysLearning Apr 01 '23 at 12:17
  • @AlwaysLearning The issue is it sometimes work without any change. Can there be anything related to avalability groups etc. i don't know the current setup but I can ask questions to DBA – Zubaer Naseem Apr 02 '23 at 08:41
  • Clusters, Availability Groups, or database load balancers (e.g.: ScaleArc) are all important things that need be mentioned in your question if they could be part of the issue. Seems like you need to sit down with the DBA to prepare logical and physical network layout diagrams to figure out what part(s) could be contributing to intermittent connections. – AlwaysLearning Apr 02 '23 at 09:04
  • I have requested more information about sql setup but issue is why this error is not coming with older .net 4.8. To test, I created 2 applications that just open and close connection. Only difference is one compiled with 4.8 using System.Data.SqlClient and other using dotnet 6 with Microsoft.Data.SqlClient. The application built using 4.8 is able to open connection with db but dotnet 6 one gets this error after timeout. In both I used same connection string with encrypt=false option – Zubaer Naseem Apr 05 '23 at 12:13
  • @ZubaerNaseem , Is your problem solved? – Amin Rousta Jul 16 '23 at 08:18
  • @Amin Rousta, no – Zubaer Naseem Aug 02 '23 at 07:46

0 Answers0