Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error occurred during the pre-login handshake

Please read in the entirety before marking this as duplicate.

In a project that I am debugging I receive a SqlException saying the following:

Additional information: A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: SSL Provider, error: 0 - The wait operation timed out.)

This occurred during a debugging session where the previous session executed only seconds before without problem. Since the initial exception, I am unable to connect to the database server in this project. The exception is thrown on the SqlConnection.Open() method call.

The Background

This is not the first time that I have received this. Previously I struggled with it for two weeks eventually initiating a Microsoft support ticket for it. In that instance it turned out the ApplicationName property on the connection string was too long (we were using the fully qualified assembly name) and shortening it alleviated the problem.

This time around, there is

  • No ApplicationName value supplied
  • WinSocks is in its default state
  • Antivirus (ESET) was disabled and was not the issue.
  • Nothing was installed between a working and non-working debug session

Finally, on a whim, I created a new project whose sole purpose was to connect to this same SQL server. I copied the connection string from the non-working project, into the new project and it connects. Is there some kind of per-project connection caching going on? Something that survives a Clean>Rebuild and a restart of Visual Studio and Windows too?

Relevant Code

    public SqlConnection OpenSqlConnection(string connectionString)
    {
        var conn = new SqlConnection(connectionString);
        conn.Open();
        _connectionString = connectionString;
        var sb = new SqlConnectionStringBuilder(_connectionString);
        _server = sb.DataSource;
        _database = sb.InitialCatalog;
        return conn;
    }

The connection string that is being passed in is output from a SqlConnectionStringBuilder elsewhere in the application. The connection string is similar to: "Data Source=SERVER;Initial Catalog=DATABASE;Integrated Security=True;Connect Timeout=60"

like image 745
CodeWarrior Avatar asked Apr 24 '17 20:04

CodeWarrior


People also ask

What is pre login handshake?

means that the client application was able to complete the TCP 3-way handshake properly (hence you notice “ A connection was successfully established with the server ”), but during the pre-login handshake, the client application checks with the SQL Server on the TDS protocol version to be used henceforth for the ...


3 Answers

What did the trick for me is increasing the timeout on the connection string, since when connecting by vpn it took to long to establish the connection. You can do this by adding ;connection timeout = value

I got the same error when connecting an application tried to connect to sql server while I was on a vpn.

By default the timeout is set to 15 seconds.

Seems you already have 60secs, maybe you just need more...

Hope it helps!

like image 73
StefanE Avatar answered Oct 19 '22 03:10

StefanE


This problem can be related to a firewall in the middle that is doing SSL inspection.

I Suggest you either try again using another connection not doing SSL inspection, or ask your firewall admin to create an exemption for the source and/or destination you are connecting to,

Cheers!

like image 34
procloudadmin Avatar answered Oct 19 '22 01:10

procloudadmin


Try explicitly adding Application Name=MyAppName; to the connection string. Auto-generated value from the assembly name might exceed some limit.

Check network settings for things like explicitly limited frame size. Reboot router if SQL Server is running on another machine.

Try adding Pooling=False; to the connection string and checking whether this solves the problem with repeated connections on application restart.

like image 2
Y.B. Avatar answered Oct 19 '22 02:10

Y.B.