我在 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=0 和 commandtimeout=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/