自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

關(guān)閉WCF鏈接正確方法詳解

開(kāi)發(fā) 開(kāi)發(fā)工具
關(guān)閉WCF鏈接是在實(shí)際編程中比較重要的一個(gè)方法。在這里我們就為大家詳細(xì)介紹一下有關(guān)關(guān)閉WCF鏈接的正確方法,方便大家學(xué)習(xí)。

WCF是一款由微軟開(kāi)發(fā)的有關(guān)通信的一個(gè)開(kāi)發(fā)框架。它可以為我們創(chuàng)造一個(gè)可靠的,跨平臺(tái)的方式。通常情況下我們關(guān)閉WCF鏈接都是簡(jiǎn)單地寫(xiě)把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)題。

我們可以寫(xiě)一個(gè)擴(kuò)展來(lái)專(zhuān)門(mén)關(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. }  

 

責(zé)任編輯:曹凱 來(lái)源: 博客園
相關(guān)推薦

2010-02-26 11:22:16

LitwareHR使用

2010-02-22 14:28:35

WCF實(shí)現(xiàn)loadin

2010-02-26 09:33:18

WCF創(chuàng)建WebSer

2010-03-01 16:59:31

WCF異常調(diào)試

2010-02-24 10:35:56

WCF鏈接服務(wù)超時(shí)

2010-02-23 17:59:52

WSIT連接WCF

2010-03-01 10:26:40

WCF異步服務(wù)

2010-02-25 16:07:28

WCF REST

2010-02-23 17:05:38

2010-03-01 17:44:39

Silverlight

2010-02-26 11:15:51

WCF接口方法

2010-03-02 14:12:30

WCF枚舉類(lèi)型

2009-12-22 16:36:38

WCF重載

2010-02-25 13:48:23

WCF動(dòng)態(tài)創(chuàng)建代碼

2010-02-25 16:52:12

引用WCF服務(wù)

2010-02-25 09:13:34

WCF異步調(diào)用

2010-02-25 13:54:48

WCF安全參數(shù)

2010-02-23 14:48:38

WCF事件通知

2010-03-02 17:35:20

WCF服務(wù)加載

2010-02-23 10:51:32

WCF Address
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)