日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
關(guān)閉WCF鏈接正確方法詳解

WCF是一款由微軟開發(fā)的有關(guān)通信的一個(gè)開發(fā)框架。它可以為我們創(chuàng)造一個(gè)可靠的,跨平臺(tái)的方式。通常情況下我們關(guān)閉WCF鏈接都是簡(jiǎn)單地寫把ICommunicationObject.Close()方法。#t#

但是這個(gè)方法有個(gè)問(wèn)題就是當(dāng)調(diào)用發(fā)生異常時(shí),Close()會(huì)發(fā)生次生的異常,導(dǎo)致鏈接不能正常關(guān)閉。如果當(dāng)這種異常很多時(shí),必然對(duì)系統(tǒng)的穩(wěn)定性有很大的影響,所以我們必須要考慮異常發(fā)生后如何關(guān)閉鏈接的問(wèn)題。

我們可以寫一個(gè)擴(kuò)展來(lái)專門關(guān)閉WCF鏈接,而不是使用原來(lái)的Close

  1. public static void CloseConnection
    (this ICommunicationObject myServiceClient)  
  2. {  
  3. if (myServiceClient.State !=
     CommunicationState.Opened)  
  4. {  
  5. return;  
  6. }  
  7. try  
  8. {  
  9. myServiceClient.Close();  
  10. }  
  11. catch (CommunicationException ex)  
  12. {  
  13. Debug.Print(ex.ToString());  
  14. myServiceClient.Abort();  
  15. }  
  16. catch (TimeoutException ex)  
  17. {  
  18. Debug.Print(ex.ToString());  
  19. myServiceClient.Abort();  
  20. }  
  21. catch (Exception ex)  
  22. {  
  23. Debug.Print(ex.ToString());  
  24. myServiceClient.Abort();  
  25. throw;  
  26. }  

然后可以使用這個(gè)擴(kuò)展來(lái)實(shí)現(xiàn)關(guān)閉WCF鏈接:

 
 
 
  1. protected void Close(T client)  
  2. {  
  3. if (client != null)  
  4. {  
  5. IChannel iChannel = client 
    as IChannel;  
  6. if (iChannel != null)  
  7. iChannel.CloseConnection();  
  8. else  
  9. {  
  10. IDisposable iDisposable =
     client as IDisposable;  
  11. if (iDisposable != null) 
    iDisposable.Dispose();  
  12. }  
  13. }  

網(wǎng)頁(yè)標(biāo)題:關(guān)閉WCF鏈接正確方法詳解
URL網(wǎng)址:http://www.dlmjj.cn/article/dhhsdig.html