Chapter II defines the class hierarchy

In these cases, you must tell WCF which should appear in the WSDL contract contains the class. This is accomplished using KnownTypes. It can be accomplished in four ways: by adding a KnownType attribute to [DataContract], by [ServiceContract] or [OperationContract] attribute in the configuration file by adding a reference to it and its assembly, or by When generating the WSDL definition of it.
class hierarchy defined in the list
2.17, the three elements from the Price class and a subclass composition. StockPrice, inherited from Price. Namespace applied to the two classes so they perfectly legal in XML by the name of. Each element retains its own namespace.
2010 年 10 月 19 日
First, the type of reference you can have moved from code to the configuration file, in the service configuration file system.runtime.serialization some type of information has been added. Taking into account class inheritance, you need to add reference to the base class and add KnownType reference to a derived class. This list shows 2.22 when EssentialWCF.Price is the base class and EssentialWCF.StockPrice and EssentialWCF.MetalPrice is derived class, StockService the boarding of these types of DLL.
Give an example of class inheritance. If a sequence of the derived class to reach a look forward to receiving a sequence of the base class endpoint, WCF does not know how to deserialize the derived class because it is not part of the contract. Another example is a hashtable class, storage class as the other elements of their own. WSDl hashtable class will be defined, but not so the class is stored in the internal hashtable.
2.23 configuration file defines the list of KnownType Credit Card Bank did not open the card annual fee reminder is reproduced Price, StockService, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null “> in the contract to determine the derived class of the most common method is to generate it at runtime, which can be exposed by the WCF hooks to complete. [KnownType] and [ServiceKnownType] constructor accept two characters string parameter is the string serialization or deserialization returns a list of known types of method name, if you use one of the original data container, the container or you can find the database at run-time type information and exposure to them. the list 2.23 shows a simple implementation, showing the type name is hardcoded into the GetKnownTypes method instead of being dragged into the container from the outside. 2.23 runtime code list defined KnownType [DataContract (Namespace = "http://EssentialWCF/ ")] [KnownType (typeof (StockPrice))] [KnownType (typeof (MetalPrice))] public class Price {[DataMember] public double CurrentPrice; [DataMember] public DateTime CurrentTime; [DataMember] public string Currency; static Type [] GetKnownTypes () {return new Type [] {typeof (StockPrice), typeof (MetalPrice)};}} [DataContract (Namespace = "http://EssentialWCF/")] public class StockPrice: Price {[DataMember] public string Ticker ; [DataMember] public long DailyVolume;} [DataContract (Namespace = "http://EssentialWCF/")] public class MetalPrice: Price {[DataMember] public string Meta; [DataMember] public string Quality;}
list 2.20 shows the definition of base class data contract, Price, there are two classes derived from the base class, StockPrice and MetalPrice. Note that the data contractual [KnownType] attribute, which tells WCF contract when exposed to include when the XSD StockPrice and MetalPrice said. The list also includes service implementation. GetPrice operation is polymorphic, and its return value type can be StockPrice or MetalPrice, depending on which operation is requested by the client proxy code calls GetPrice structure must be cast into need to access the return value type classes, and a list defined in the contract data 2.20 KnownType 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/")] [KnownType (typeof (StockPrice))] [KnownType (typeof (MetalPrice))] public class Price {[DataMember] public double CurrentPrice; [DataMember] public DateTime CurrentTime; [DataMember] public string Currency;} [DataContract (Namespace = "http://EssentialWCF/")] public class StockPrice: Price {[DataMember] public string Ticker; [DataMember] public long DailyVolume;} [DataContract ( Namespace = "http://EssentialWCF/")] public class MetalPrice: Price {[DataMember] public string Meta; [DataMember] public string Quality;} [ServiceBehavior (Namespace = "http://EssentialWCF/Fi nanceService /") ] [ServiceContract (Namespace = "http://EssentialWCF/Fi nanceService /")] public class StockService {[OperationContract] public Price GetPrice (string id, string type) {if (type.Contains (“Stock”)) {StockPrice s = new StockPrice (); s.Ticker = id; s.DailyVolume = 45000000; s.CurrentPrice = 94.15; s.CurrentTime = DateTime.Now; s.Currency = “RMB”; return s;} if (type.Contains (“Metal”)) {MetalPrice g = new MetalPrice (); g.Meta = id; g.Quality = “0 .. 999″; g.CurrentPrice = 785.00; g.CurrentTime = DateTime.Now; g.Currency = “RMB”; return g;} return new Price ();}}} Accordingly, you can use the [ServiceKnownType] property defined in OperationContract level KnownType. When KnownTypes is defined in the operating level, the derived class can only have been defined know the type of operation used in other words, not all the operations a service can use the derived class. list 2.21 shows the use of [ServiceKnownType] code fragments in this case, when a message from the server returns a GetPrice client can call methods, deserializer will create a StockPrice or MetalPrice object, but when the client can only be passed when calling SetPrice a Price object, rather than a StockPrice or MetalPrice object, because the serializer does not know in XML how to express these derived types.
complex type in the code to the general form of the class implementation. complicated further by increasing the special structure of the class inheritance relationship defined in this way, a common type such as “price” can be derived for a more specific type, such as “stock price” or “house price”. WCF supports the WSDL through the appropriate representation of the class inheritance relationship between the class structure and the XML serialization and deserialization are also from each Remove the attribute class and add to a collection.
list defined in the contract operations 2.21 KnownType [ServiceBehavior (Namespace = "http://EssentialWCF/Fi nanceService /")] [ServiceContract (Namespace = "http:/ / EssentialWCF / Fi nanceService / ")] public class StockService {[ServiceKnownType (typeof (StockService))] [ServiceKnownType (typeof (MetalPrice))] [OperationContract] public Price GetPrice (string id, string type) {if (type.Contains (“Stock”)) {StockPrice s = new StockPrice (); s.Ticker = id; s.DailyVolume = 45000000; s.CurrentPrice = 94.15; s.CurrentTime = DateTime.Now; s.Currency = “RMB”; return s;} if (type.Contains (“Metal”)) {MetalPrice g = new MetalPrice (); g.Meta = id; g.Quality = “0 .. 999″; g.CurrentPrice = 785.00; g.CurrentTime = DateTime.Now; g.Currency = “RMB”; return g;} return new Price ();}} both in the data contract or service contract layer layer, defined in the code known disadvantage is that you need to type at compile time into the derived class to know if a new type is added, you need to recompile the code, which you can use two methods to solve.
If the data types to meet any of the conditions described earlier, then they will be exposed in the WSDL out there may be some extra, of course, you may want to force a type included in the WSDL contract
2.17 using data contract defines a list of the class hierarchy using System; using System.Collections.Generic; using System.Linq ; using System.Text; using System.Runtime.Serialization; namespace Stock {[DataContract (Namespace = "http://EssentialWCF/Price/")] public class Price {[DataMember] public double CurrentPrice; [DataMember] public DateTime CurrentTime ; [DataMember] public string Currency;} [DataContract (Namespace = "http://EssentialWCF/StockPrice")] public class StockPrice: Price {[DataMember] public string Ticker; [DataMember] public long DailyVolume; [DataMember] public double DailyChange;}} to generate these two XML metadata is used to support the basis for the list to display the relationship 2.18 First Price XML metadata display, followed by the show StockPrice XML metadata import Price StockPrice attention metadata. Note that in XSD, all elements are defined with minOccurs = 0, because they are not in the code using [isRequired = true] to define the attributes and a list 2.18 XML metadata as defined in the class hierarchy StockPrice type of a serialized SOAP message body is displayed in the list 2.19 Note Price and StockPrice namespace entirely through the SOAP message body be moved from the code list list 2.17 2.18 in the XML metadata data, and a list 2.19 SOAP message body serialization class hierarchy Dollars 100 2006-12-13T21: 18: 51.313-05:00 0.012345 450000 < / a: DailyVolume> msft KnownTypes exposed in WSDL using additional types

No comments allowed