异步调用WCF的方法需要小心的地方
生活随笔
收集整理的這篇文章主要介紹了
异步调用WCF的方法需要小心的地方
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
直接使用下面的代碼,由于client對象占用的資源沒有被釋放,會導致內存泄露
GetSimServiceReference.GetSimServiceClient client = new GetSimServiceReference.GetSimServiceClient()
client.computerSimAsync(DepartmentNo, FileID, F_intput.Length, ReadStringArrayFromStrings());
上面的問題,可以使用下面的方法來避免
using (GetSimServiceReference.GetSimServiceClient client = new GetSimServiceReference.GetSimServiceClient()){client.computerSimA(DepartmentNo, FileID, F_intput.Length, ReadStringArrayFromStrings());}
但是如果使用異步調用的方式,會導致調用不到遠程服務,并且沒有任何異常提示,很是讓人摸不到頭腦
using (GetSimServiceReference.GetSimServiceClient client = new GetSimServiceReference.GetSimServiceClient()){client.computerSimAsync(DepartmentNo, FileID, F_intput.Length, ReadStringArrayFromStrings());}
?
最后,采用了如下的代碼:
? ? ? ? ? ? ? ? ?
try{GetSimServiceReference.GetSimServiceClient client = new GetSimServiceReference.GetSimServiceClient();client.computerSim(DepartmentNo, FileID, F_intput.Length, ReadStringArrayFromStrings());if (client.State != CommunicationState.Faulted) client.Close();}catch (CommunicationObjectFaultedException cofe){ShowException.SaveLogAsTXTInfoex("調用WCF服務失敗,異常信息:" + cofe.Message);}catch (Exception ex){ShowException.SaveLogAsTXTInfoex("調用WCF服務失敗,異常信息:" + ex.Message+f_intput.FullName);}
?
轉載于:https://www.cnblogs.com/goxmpx/p/3314184.html
總結
以上是生活随笔為你收集整理的异步调用WCF的方法需要小心的地方的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 古惑仔电影的事情不会都是真的吧我哥哥说都
- 下一篇: 英文女生个性签名带翻译