Service contracts
A service contract specifies the signature of a service, the data it exchanges, and other contractually required data. Services are groups of operations. To create a service contract, you must model operations and specify their grouping. In models of WCF applications, first create a service contract by modeling an interface and applying the «ServiceContract» stereotype. Then, define the operations by creating a UML operation, stereotyped as a C# method, and applying the «OperationContract» stereotype.
Any operations that do not have «OperationContract» stereotype are not service operations, and after code generation, are not exposed for use by clients of WCF services. They can only be called by objects within their declared access scope.
Message contracts
Used to support the schema generation of SOAP messages. Typically, you will use a message contract if you want to define a message that includes custom SOAP headers or where you want to compose a message from multiple data contract types.
When a SOAP schema is generated by the data contract serializer, you must follow specific rules. For example, the SOAP body element in WS-I Basic Profile–compliant services can only contain one child element. In many cases, you may need to control that schema instead of using the default behavior of the data contract serializer, which is where the MessageContract attribute comes into play.
The MessageContract attribute is used on a class that defines the data types or contracts that will be added to a message header or body. The following code example shows a data contract class named SearchCriteria that is included in a message contract class named FindEmployeeRequest.
Data contracts
A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged. That is, to communicate, the client and the service do not have to share the same types, only the same data contracts. A data contract precisely defines, for each parameter or return type, what data is serialized (turned into XML) to be exchanged.
Data Contract Basics
Windows Communication Foundation (WCF) uses a serialization engine called the Data Contract Serializer by default to serialize and deserialize data (convert it to and from XML). All .NET Framework primitive types, such as integers and strings, as well as certain types treated as primitives, such as DateTime and XmlElement, can be serialized with no other preparation and are considered as having default data contracts. Many .NET Framework types also have existing data contracts. For a full list of serializable types, see Types Supported by the Data Contract Serializer.
New complex types that you create must have a data contract defined for them to be serializable. By default, the DataContractSerializer infers the data contract and serializes all publicly visible types. All public read/write properties and fields of the type are serialized. You can opt out members from serialization by using the IgnoreDataMemberAttribute. You can also explicitly create a data contract by using DataContractAttribute and DataMemberAttribute attributes. This is normally done by applying the DataContractAttribute attribute to the type. This attribute can be applied to classes, structures, and enumerations. The DataMemberAttribute attribute must then be applied to each member of the data contract type to indicate that it is a data member, that is, it should be serialized. For more information, see Serializable Types.