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.