2017-06-12 48 views
0

使用AsterNet連接到Asterisk的,我已經看到了以下內容:如何從AsterNet防止或捕獲「無法運行:套接字爲空」?

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() 

你可以看到,引發此異常在ManagerReader.cs的AsterNet代碼行,行197.它看起來像這種情況當connect()(在ManagerConnection.cs中)啓動一個新線程,但mrSocket在ManagerReader.cs中某種程度上是空的。但是這個異常是從新線程拋出的,所以我無法捕捉它並處理錯誤。它只是使我的應用程序崩潰。

所以我有兩個問題。首先,我如何防止這種情況發生?我是如何連接或重新連接的?其次,是否有可能以某種方式捕捉到這種情況並再次嘗試連接?

+0

而不共享您的登錄代碼,而實際上是造成這種異常被拋出的代碼的一個例子,它是很難給出任何除了鏈接到AsterNet源代碼之外的其他答案。 – Deantwo

+0

https://github.com/AsterNET/AsterNET/blob/master/Asterisk.2013/Asterisk.NET/Manager/ManagerReader.cs#L197 – Deantwo

+0

如果其他人很好奇:https://github.com/AsterNET/AsterNET /問題/ 107 – Deantwo

回答

0

發生這種情況是因爲我試圖維持自己的連接,我不必這樣做。我編寫了一個循環來檢查連接,並且每次都檢查連接,如果連接丟失並重新連接,併發送ping命令以保持連接。你可以看到我在這裏做什麼一個例子:

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

但我也沒必要那樣做。 AsterNet的當前版本爲您做了所有這些工作。當我還試圖手動完成時,它正在干擾圖書館正在採取的步驟。所以底線是,讓AsterNet庫爲你保持連接。

你可以看到這裏的維護者我的討論:

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