Wednesday, October 22, 2008

WCF Contracts

The WCF way to generate an XML schema from a CLR type is with a data contract. Data contracts describe how a CLR type maps to schema with an opt-in approach. The XmlFormatter uses the information provided in a data contract to handle serialization and deserialization. This allows developers to work with familiar objects at runtime, while the WCF runtime hides the XML goo. The XmlFormatter is the successor of the XmlSerializer for WCF services - with the exception that currently, the XmlSerializer still provides more granular control over object serialization to XML. As such there will still be cases where you will still want to use the XmlSerializer, for now.
Creating a Data Contract
First you decorate the type with the DataContractAttribute, then you select members to include in serialization with the DataMemberAttribute. Listing 3 illustrates the LinkItem type mentioned earlier, as a data contract.


Service Contracts
In the WCF, service contracts describe the operations supported by a service, the message exchange pattern they use, and the format of each message. The service contract is the driver for generating a service description – so a service must implement at least one service contract. To create a service contract you define an interface with related methods representative of a collection of service operations, and then decorate the interface with the ServiceContractAttribute to indicate it is a service contract. Methods in the interface that should be included in the service contract are decorated with the OperationContractAttribute. Listing 1 illustrates the use of these attributes

No comments:

Post a Comment