vb.net - npgsql 连接等待 ssl

标签 vb.net ssl npgsql

我在 vb.net 中通过 npgsql 设置了一个监听器以接收来自 postresql 的通知。 sub 只是打开一个新的 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()

时失败

Wait() with timeout isn't supported when SSL is used, see https://github.com/npgsql/npgsql/issues/1501

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

in Npgsql.NpgsqlConnector.Wait(Int32 timeout)

in Npgsql.NpgsqlConnection.Wait(Int32 timeout)

in Npgsql.NpgsqlConnection.Wait()

有人能帮忙吗?

最佳答案

我找到了解决方案。我写在这里以防万一其他人有同样的问题。

我将 Wait() 更改为 WaitAsync()

为了避免连续循环,还添加了Await并将子声明为Async

下面是代码

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

关于vb.net - npgsql 连接等待 ssl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53832032/

相关文章:

.net - 从 .NET 逆向工程 Access 宏

ssl - 如何在 Go 中访问客户端的证书?

ssl - handshake_failure 试图访问同一服务器上的文件

C# 和 NpgsqlDataAdapter 返回单个字符串而不是数据表

c# - 在 Entity Core 中使用 Postgres lpad

c# - Postgresql 上的 CaSTLe Activerecord 错误是 “relation does not exist”?

vb.net - 创建 Application.DoEvents 的等待延迟替代方案

vb.net - 处置所有命令后数据库也被锁定

vb.net - 理解一段代码

ruby - 使用 ruby​​gem 访问 Shopify API 时出现 SSL_connect 错误