npgsql连接与ssl等待

时间:2018-12-18 11:27:49

标签: vb.net ssl npgsql

我设置了一个侦听器,以通过vb.net中的npgsql从postresql发出通知。子程序只是打开一个新的ssl连接,并使用一个循环等待通知来启动新线程。下面是代码

Public Sub StartListening()
    If mConnString Is Nothing Then
        Main_Form.WriteMessage("Not connected")
    End If
    Try
        connection = New NpgsqlConnection(mConnString) With {
            .ProvideClientCertificatesCallback = New ProvideClientCertificatesCallback(AddressOf MyProvideClientCertificates)
        }
        connection.Open()
        If connection.State = ConnectionState.Open Then
            Using command = New NpgsqlCommand("listen my_notification", connection)
                command.ExecuteNonQuery()
            End Using
        End If

        AddHandler connection.Notification, New NotificationEventHandler(AddressOf OnNotification)
        Dim thread As New Thread(
            Sub()
                While True
                    connection.Wait()
                End While
            End Sub
        )
        thread.Start()

    Catch ex As Exception
        Main_Form.WriteMessage("Error:" +  ex.Message)
    End Try
End Sub


Private Sub MyProvideClientCertificates(ByVal clienteCertis As X509CertificateCollection)
    Dim cert As X509Certificate2 = New X509Certificate2("mycertificate.pfx")
    clienteCertis.Add(cert)
End Sub

一切正常,直到我引入SSL连接: connection.Wait()

失败
  

使用SSL时不支持带有超时的Wait(),请参阅   https://github.com/npgsql/npgsql/issues/1501

因为实际上我不需要超时,所以我尝试在连接字符串中设置 timeout = 0 commandtimeout = 0 ,但是错误仍然存​​在,这就是我所看到的错误堆栈跟踪

  

在Npgsql.NpgsqlConnector.Wait(Int32超时)中

     

在Npgsql.NpgsqlConnection.Wait(Int32超时)中

     

在Npgsql.NpgsqlConnection.Wait()

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我写这里是为了防止其他人遇到同样的问题。

我用 WaitAsync()更改了 Wait()

为避免连续循环,还添加了等待,并将子项声明为异步

下面是代码

        ....
        Dim thread As New Thread(
            Async Sub()
                While True
                    Await connection.WaitAsync()
                End While
            End Sub
        )
        thread.Start()
        ....