登录前握手期间发生错误

时间:2022-01-01 01:55:14

Please read in the entirety before marking this as duplicate.

在将此标记为重复之前,请完整阅读。

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

在我正在调试的项目中,我收到一个SqlException,说明如下:

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

附加信息:已成功与服务器建立连接,但在登录前握手期间发生错误。 (提供者:SSL提供者,错误:0 - 等待操作超时。)

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.

这发生在调试会话期间,前一个会话仅在几秒钟之前执行而没有问题。自初始异常以来,我无法连接到此项目中的数据库服务器。 SqlConnection.Open()方法调用抛出异常。

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.

这不是我第一次收到这个。在此之前,我花了两个星期的努力才最终为它启动微软支持票。在那个例子中,事实证明连接字符串上的ApplicationName属性太长(我们使用的是完全限定的程序集名称)并缩短它可以缓解问题。

This time around, there is

这一次,有

  • No ApplicationName value supplied
  • 没有提供ApplicationName值
  • WinSocks is in its default state
  • WinSocks处于默认状态
  • Antivirus (ESET) was disabled and was not the issue.
  • 防病毒(ESET)已禁用,不是问题。
  • 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?

最后,我突发奇想,创建了一个新项目,其唯一目的是连接到同一个SQL服务器。我将连接字符串从非工作项目复制到新项目中并连接。是否存在某种每个项目的连接缓存?在Clean> Rebuild以及Visual Studio和Windows的重启中幸存下来的东西呢?

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"

传入的连接字符串是从应用程序中其他位置的SqlConnectionStringBuilder输出的。连接字符串类似于:“Data Source = SERVER; Initial Catalog = DATABASE; Integrated Security = True; Connect Timeout = 60”

3 个解决方案

#1


2  

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

尝试显式添加Application Name = MyAppName;到连接字符串。程序集名称中的自动生成值可能会超出某个限制。

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

检查网络设置是否有明确限制的帧大小。如果SQL Server在另一台计算机上运行,​​请重新启动路由器。

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

尝试添加Pooling = False;到连接字符串并检查这是否解决了应用程序重启时重复连接的问题。

#2


0  

So this issue continued to plague me and it appears that it was a function of lag from my home network (I am a remote developer) and the work network accessed over a VPN. I have a 100ms average ping time to servers on the work network.

所以这个问题继续困扰着我,看来这是我家网络(我是一个远程开发人员)和通过VPN访问的工作网络的滞后功能。我对工作网络上的服务器的平均ping时间为100毫秒。

The real oddity is that the connection string worked without problem for months, then suddenly stopped. At the time, the Application Name value was something like Application Name = "MyProgram.DAL. Culture=en, PublicKeyToken=1a1a1a1a1a1a1a1a, Version=1.0.0.0". In other words a fully qualified assembly name. Eventually I changed this to the shortened "MyProgram.DAL" type name and it worked again.

真正奇怪的是连接字符串工作几个月没有问题,然后突然停止。当时,Application Name值类似于Application Name =“MyProgram.DAL.Culture = en,PublicKeyToken = 1a1a1a1a1a1a1a1a,Version = 1.0.0.0”。换句话说,一个完全限定的程序集名称。最后我将其更改为缩短的“MyProgram.DAL”类型名称,它再次起作用。

Some months later, again I was beset by it. And I happened to find that if I just ate the exception and waited a few ticks, everything was fine. The application would happily use the connection even though it reported that it failed. Thus, I changed the method to the below:

几个月后,我又被它困扰了。我碰巧发现,如果我只吃异常并等了几滴,一切都很好。应用程序很乐意使用连接,即使它报告它失败了。因此,我将方法改为:

    public SqlConnection OpenSqlConnection(string connectionString)
    {
        var conn = new SqlConnection(connectionString);
        var retries = 10;
        while (conn.State != ConnectionState.Open && retries > 0)
        {
            try
            {
                conn.Open();
            }
            catch (Exception)
            {

            }
            Thread.Sleep(500);
            retries--;
        }

        _connectionString = connectionString;
        var sb = new SqlConnectionStringBuilder(_connectionString);
        _server = sb.DataSource;
        _database = sb.InitialCatalog;
        return conn;
    }

#3


0  

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

对我来说,诀窍是增加连接字符串的超时,因为当通过vpn连接时,建立连接需要很长时间。您可以通过添加; connection timeout = value来完成此操作

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

当我在一个VPN上连接一个试图连接到sql server的应用程序时,我遇到了同样的错误。

By default the timeout is set to 15 seconds.

默认情况下,超时设置为15秒。

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

似乎你已经有60秒了,也许你只需要更多......

Hope it helps!

希望能帮助到你!

#1


2  

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

尝试显式添加Application Name = MyAppName;到连接字符串。程序集名称中的自动生成值可能会超出某个限制。

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

检查网络设置是否有明确限制的帧大小。如果SQL Server在另一台计算机上运行,​​请重新启动路由器。

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

尝试添加Pooling = False;到连接字符串并检查这是否解决了应用程序重启时重复连接的问题。

#2


0  

So this issue continued to plague me and it appears that it was a function of lag from my home network (I am a remote developer) and the work network accessed over a VPN. I have a 100ms average ping time to servers on the work network.

所以这个问题继续困扰着我,看来这是我家网络(我是一个远程开发人员)和通过VPN访问的工作网络的滞后功能。我对工作网络上的服务器的平均ping时间为100毫秒。

The real oddity is that the connection string worked without problem for months, then suddenly stopped. At the time, the Application Name value was something like Application Name = "MyProgram.DAL. Culture=en, PublicKeyToken=1a1a1a1a1a1a1a1a, Version=1.0.0.0". In other words a fully qualified assembly name. Eventually I changed this to the shortened "MyProgram.DAL" type name and it worked again.

真正奇怪的是连接字符串工作几个月没有问题,然后突然停止。当时,Application Name值类似于Application Name =“MyProgram.DAL.Culture = en,PublicKeyToken = 1a1a1a1a1a1a1a1a,Version = 1.0.0.0”。换句话说,一个完全限定的程序集名称。最后我将其更改为缩短的“MyProgram.DAL”类型名称,它再次起作用。

Some months later, again I was beset by it. And I happened to find that if I just ate the exception and waited a few ticks, everything was fine. The application would happily use the connection even though it reported that it failed. Thus, I changed the method to the below:

几个月后,我又被它困扰了。我碰巧发现,如果我只吃异常并等了几滴,一切都很好。应用程序很乐意使用连接,即使它报告它失败了。因此,我将方法改为:

    public SqlConnection OpenSqlConnection(string connectionString)
    {
        var conn = new SqlConnection(connectionString);
        var retries = 10;
        while (conn.State != ConnectionState.Open && retries > 0)
        {
            try
            {
                conn.Open();
            }
            catch (Exception)
            {

            }
            Thread.Sleep(500);
            retries--;
        }

        _connectionString = connectionString;
        var sb = new SqlConnectionStringBuilder(_connectionString);
        _server = sb.DataSource;
        _database = sb.InitialCatalog;
        return conn;
    }

#3


0  

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

对我来说,诀窍是增加连接字符串的超时,因为当通过vpn连接时,建立连接需要很长时间。您可以通过添加; connection timeout = value来完成此操作

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

当我在一个VPN上连接一个试图连接到sql server的应用程序时,我遇到了同样的错误。

By default the timeout is set to 15 seconds.

默认情况下,超时设置为15秒。

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

似乎你已经有60秒了,也许你只需要更多......

Hope it helps!

希望能帮助到你!