【发布时间】:2023-03-28 23:27:01
【问题描述】:
我想捕获当我尝试使用 Neo4jClient.dll 连接到我的 neo4j 数据库时发生的异常。如果数据库处于脱机状态,我会收到以下错误:“在 mscorlib.dll 中发生了‘System.AggregateException’类型的异常,但未在用户代码中处理。”我的 catch-block 永远不会到达。
这是我的代码:
class Neo4JConnector
{
private static GraphClient client = null;
public Neo4JConnector(IniConfigSource configSource)
{
if (client == null)
{
client = new GraphClient(new Uri(configSource.Configs["Configuration"].Get("Neo4jUrl")));
try
{
client.Connect();
}
catch (Exception ex)
{
Console.WriteLine("Cannot connect"); // never reached :(
}
然后我尝试在这段代码中使用“extern”修饰符:
class Neo4JConnector
{
private static GraphClient client = null;
[DllImport("Neo4jClient.dll", EntryPoint="Connect")]
static extern void Connect();
public Neo4JConnector(IniConfigSource configSource)
{
if (client == null)
{
client = new GraphClient(new Uri(configSource.Configs["Configuration"].Get("Neo4jUrl")));
try
{
Connect();
}
catch (Exception ex)
{
Console.WriteLine("Cannot connect");
}
}
但我得到的只是一个异常,上面写着“[System.EntryPointNotFoundException] = {“无法在 DLL 'Neo4jClient.dll' 中找到名为 'Connect' 的入口点。”:““}”
这是 Neo4jClient.dll 中签名的样子
public virtual void Connect();
我的代码有什么问题?有没有更好的方法来捕获外部异常?请帮忙:(
【问题讨论】:
-
如果它的签名说是“虚拟的”,那么您可以根据需要覆盖它,包括连接失败的所需 try{}catch{}
-
能否提供完整的异常堆栈跟踪?
标签: c# dll exception-handling neo4jclient