我如何阻止或捕获来自 AsterNet 的 "Unable to run: socket is null"?

How do I prevent or catch "Unable to run: socket is null" from AsterNet?

使用 AsterNet 连接到 Asterisk,我看到了以下内容:

Unhandled Exception:

System.SystemException: Unable to run: socket is null.
at AsterNET.Manager.ManagerReader.Run() in
e:\Projects\Github\AsterNET\Asterisk.2013\Asterisk.NET\Manager\ManagerReader.cs:line 197
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback,
Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state,
Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

您可以在 AsterNet 代码 ManagerReader.cs 的第 197 行看到抛出此异常的行。看起来这发生在 connect()(在 ManagerConnection.cs 中)启动一个新线程时,但是 mrSocket 在 ManagerReader.cs 中不知何故为空。但是这个异常是从那个新线程抛出的,所以我无法捕获它并处理错误。它只是让我的应用程序崩溃。

所以我有两个问题。首先,如何防止这种情况发生?我连接或重新连接的方式有问题吗?其次,是否有可能以某种方式捕获并再次尝试连接?

发生这种情况是因为我试图自己维护连接,但我没有必要这样做。我写了一个循环来经常检查连接,如果连接断开则重新连接并发送 ping 以保持连接。您可以在此处查看我所做的示例:

https://gist.github.com/squaregear/80821130d221727c3836dba3224bf93e

但我不需要那样做。 AsterNet 的当前版本为您完成了所有这些工作。当我也尝试手动执行时,它会干扰图书馆正在采取的步骤。所以最重要的是,让 AsterNet 库为您维护连接。

你可以在这里看到我与维护者的讨论:

https://github.com/AsterNET/AsterNET/issues/107