2010-12-22 50 views
0

爲什麼這個具體的WCF客戶端實現確實實現了IDisposable,但不是作爲.net的一部分爲每個wcf客戶端提供的默認開箱即用接口?WCF IDisposable設計差異

using(WcfClient client = new WcfClient) 
    { 
     client.WebMethod(); 
    } 


    using(IWcfClient client = new WcfClient) 
    { 
     client.WebMethod(); 
    } //throws bc it's not part of the Interfact but is the part of the base class every wcf client implements along the interface? 

爲什麼不讓它的界面的一部分,人們可以選擇如何處理自定義的方式清理WCF?

+0

在切線筆記,請注意混合帶有`使用'塊的WCF客戶端可以屏蔽異常。 http://stackoverflow.com/questions/573872/what-is-the-best-workaround-for-the-wcf-client-using-block-issue – 2010-12-22 17:50:59

回答

1

這是因爲接口是Web服務的數據契約,它對於數據契約實現IDisposable沒有意義。數據合同包含運營合同,沒有什麼可處置的。這是需要處理的通道,這就是自動生成的代理類(WcfClient)實現IDisposable的原因。

你可以試試這個,如果你堅持要用你的數據合同:

IWcfClient client = new WcfClient(); 
using((IDisposable)client) 
{ 
    client.WebMethod(); 
} 

,但我實在看不出有什麼不對:

using(var client = new WcfClient()) 
{ 
    client.WebMethod(); 
} 
+0

是的,客戶端的具體實例如何不是封裝2端點通道。那麼它與界面有什麼不同呢? – dexter 2010-12-22 17:04:26