深入講解WCF集合類型
WCF經(jīng)過長時間的發(fā)展,很多用戶都很了解WCF了,這里我發(fā)表一下個人理解,和大家討論討論。首先先了解一下WCF的發(fā)展,自從在微軟提出.NET戰(zhàn)略以來,就針對建立企業(yè)級的分布式應(yīng)用先后推出了一系列產(chǎn)品和技術(shù),包括:ASP.NET Web服務(wù)、.NET Remoting、Message Queuing以及Enterprise Service等。這些技術(shù)為基于微軟技術(shù)的軟件研發(fā)人員開發(fā)分布式應(yīng)用提供了很大的便利,同時也各自存在著一些不足。
#T#WCF自定義集合類型,如果作為服務(wù)契約的一部分進(jìn)行發(fā)布,必須要保證以下幾點(diǎn):WCF集合包含的類型必須使用[Serializable]和[DataContract]標(biāo)記;WCF集合包含的類型屬性必須使用[DataMember]標(biāo)記,并且,如果是屬性(Property),必須要實(shí)現(xiàn)get和set;WCF集合類型必須使用[Serializable]和[CollectionDataContract]標(biāo)記,以及[KnownType]標(biāo)記指向集合包含的子類型;集合類型必須實(shí)現(xiàn)IEnumerable<T>接口;集合類型使用[DataMember]標(biāo)記的IList將集合項向客戶端公開.
樣例如下:
- namespaceSharpnessdotnet
- {
- [Serializable]
- [DataContract]
- publicclassSharpnessdotnet
- {
- privatestringname;
- [DataMember]
- publicstringName
- {
- get
- {
- returnname;
- }
- set
- {
- name=value;
- }
- }
- }
- [Serializable]
- [CollectionDataContract]
- [KnownType(typeof(Sharpnessdotnet))]
- publicclassSharpnessdotentCollection:IEnumerable<Sharpnessdotnet>
- {
- [DataMember]
- publicIList<Sharpnessdotnet>List;
- publicSharpnessdotentCollection()
- {
- List=newList<Sharpnessdotnet>();
- }
- publicvoidAdd(Sharpnessdotnetobj)
- {
- List.Add(obj);
- }
- publicIEnumerator<Sharpnessdotnet>GetEnumerator()
- {
- returnList.GetEnumerator();
- }
- IEnumeratorIEnumerable.GetEnumerator()
- {
- returnList.GetEnumerator();
- }
- }
- }