對WCF客戶端解釋說明
WCF客戶端包括許多新的和改進的用于提高工作效率的功能。IDE 還進行了重新設(shè)計,從而向開發(fā)人員提供對.NET Framework 組件的直接訪問。
[注:Method、Operation 中文都叫做「方法」,但前者是存在 OO 中的類,不存在網(wǎng)絡(luò)上;后者存在于 Service 中,公開在網(wǎng)絡(luò)上可供其他程序調(diào)用。WCF、Data Services 和 RIA Services 中公開在網(wǎng)絡(luò)上的函數(shù)和方法,都可稱作 Operation。#t#
關(guān)于這點,小弟我查了微軟 MCTS 認(rèn)證 WCF 3.5 的官方用書 [10]、O'Reilly 的書籍 [11],都未提到如何解決,書中只提到 .NET collections 的 metadata,以 WSDL 在網(wǎng)絡(luò)上傳輸時,會以「數(shù)組 (array)」的格式呈現(xiàn)。
Because .NET collections are .NET-specific, WCF cannot expose them in the service metadata, yet because they are so useful, WCF offers dedicated marshaling rules for collections.
Whenever you define a service operation that uses the collection interfaces IEnumerable<T>, IList<T>, or ICollection<T>, the specific collection-type information gets lost in the metadata (WSDL) export, so in terms of how collection types are sent across the wire, they all are represented as arrays, the resulting metadata always uses an array.
開發(fā)WCF客戶端 時,若 VS 2008 都用默認(rèn)配置,則當(dāng) WCF 的服務(wù)器端函數(shù) (Operation) 的返回類型為 List<string> 時,實際返回的類型為 string[] 數(shù)組,因此客戶端若仍用 List<string> 的變量去接收和賦值時,在編譯時期,即會發(fā)生下圖 1 的轉(zhuǎn)型錯誤:
WCF 客戶端程序「添加服務(wù)引用 (Add Service Reference)」的設(shè)置即可處理此種需求。做法如下:
請參閱本帖。當(dāng)我們的WCF客戶端程序,要引用網(wǎng)絡(luò)上既有的 WCF 服務(wù)契約時,我們會如下圖 2 般,添加一個 service proxy reference。