對WCF客戶端解釋說明
WCF客戶端包括許多新的和改進的用于提高工作效率的功能。IDE 還進行了重新設計,從而向開發人員提供對.NET Framework 組件的直接訪問。
[注:Method、Operation 中文都叫做「方法」,但前者是存在 OO 中的類,不存在網絡上;后者存在于 Service 中,公開在網絡上可供其他程序調用。WCF、Data Services 和 RIA Services 中公開在網絡上的函數和方法,都可稱作 Operation。#t#
關于這點,小弟我查了微軟 MCTS 認證 WCF 3.5 的官方用書 [10]、O'Reilly 的書籍 [11],都未提到如何解決,書中只提到 .NET collections 的 metadata,以 WSDL 在網絡上傳輸時,會以「數組 (array)」的格式呈現。
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.
開發WCF客戶端 時,若 VS 2008 都用默認配置,則當 WCF 的服務器端函數 (Operation) 的返回類型為 List<string> 時,實際返回的類型為 string[] 數組,因此客戶端若仍用 List<string> 的變量去接收和賦值時,在編譯時期,即會發生下圖 1 的轉型錯誤:
WCF 客戶端程序「添加服務引用 (Add Service Reference)」的設置即可處理此種需求。做法如下:
請參閱本帖。當我們的WCF客戶端程序,要引用網絡上既有的 WCF 服務契約時,我們會如下圖 2 般,添加一個 service proxy reference。