2.27 a server from the list to expose a collection using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.Serialization; using System.ServiceModel; namespace EssentialWCF {[DataContract (Namespace = " http://EssentialWCF ")] public class StockPrice {[DataMember] public double CurrentPrice; [DataMember] public DateTime CurrentTime; [DataMember] public string Ticker;} [CollectionDataContract] public class StockPriceCollection: List
list 2.27 shows a collection of data using the contract and operation. This collection uses [CollectionDataContract] property to qualify, it is especially used to provide this purpose a special property of the WCF. This property is any guide WCF to support the IEnumerable interface and implements the Add method of an array type sequence into XML. StockPriceCollection class inherits from the List class, List class inherits from the base class interface to implement ICollection serialization. equivalent data contract
If you are using WCF to expose service and use svcutil.exe to create access service code, under normal circumstances you do not care about the client and service side of the line between the transmission of messages said. To know a WCF data contract. NET type sequence into an XML message set and talk about a sequence of XML information sets into a counter. NET type. XML Information Set be online to file or binary code, which depends on the communication process used in the binding, but again,. NET code is not aware of the existence of coding. This way you like to use in your code. NET type, but a set of standards-based XML encoding of that information on the specific transmission line.
2010 年 10 月 21 日
collection is. NET is very convenient data structure, which focused on the dynamic memory allocation, point to enumerate and list the advantages. Although useful, but there is no XSD or a set of standards and WCF equivalent. So, say a collection of serialized XML, WCF needs to be set in accordance with an array of treatment. In fact, a collection of online serialization level is consistent with an array. In addition to the collection (the type that inherits from ICollection
List 2.26 equivalent data contract using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.Serialization; namespace EssentialWCF {[DataContract (Namespace = "http://EssentialWCF")] public class StockPriceSvc / / V1 {[DataMember] public double CurrentPrice; [DataMember] public DateTime CurrentTime; [DataMember] public string Ticker; [DataMember] public string Currency;} [DataContract (Namespace = "http://EssentialWCF", Name = "StockPriceSvc")] public class StockPrice / / V2 {[DataMember (Order = 2)] public double CurrentPrice; [DataMember (Order = 3)] public DateTime CurrentTime; [DataMember (Order = 4)] public string Ticker; [DataMember (Order = 1, Name = "Currency ")] public string Money;}} to use set
However, there are many cases when you want the client and server use different types. If the client and server are developed by different organizations, then it is likely, or just the communication is the use of unilateral WCF. In fact, if you do not apply, or add a service reference svcutil.exe to generate the client proxy, the client will be a great member of a member name and server name different situations may occur. However, by using the [DataMember] attribute to control these names, you can let them in the XML representation is the same. As long as the client and server use an equivalent XML representation of the WCF, the deserialize the XML into a different message. NET type is possible. If the two sequences into the same class of XML metadata, data contracts that these classes can be considered the same. Contract for the data to be consistent, they must have the same namespace, name and members. XML data members must be the same name and in the same position. Brief that they have online are indistinguishable.