A WCF Service is comprised of the following major components. The diagram below shows how the components are related to each other:
- Service Contract
- Operation Contract
- Data Contract
- Data Member
Service Contract
Service contract is a contract that specifies the direction and type of the messages in a conversation. It is an interface or a class that defines the service contract in a Windows Communication Foundation (WCF) application. A service contract is the gateway to a service for external applications to make use of the service functions, and at least one service contract should be available in a service. A service contract is defined as follows:
[ServiceContract]
public interface IStudentService
{
}
The ServiceContract attribute of the interface defines the service contract in the service interface. The service contract defines the operations available in the service, operations like web service methods in a web service. IstudentService is a student service interface which exposes all the operation contracts or methods in this service to external systems.
Operation Contract
An operation contract defines the methods of the service that are accessible by external systems. The OperationContract attribute needs to be applied for all these methods, these are also like web methods in a web service. Operation contracts are defined as follows:
[ServiceContract]
public interface IStudentService
{
[OperationContract]
String GetStudentFullName (int studentId);
[OperationContract]
StudentInformation GetStudentInfo (int studentId); }
Data Contract
A data contract defines a type with a set of data members or fields that will be used as the composite type in a service contract. It is a loosely-coupled model that is defined outside the implementation of the service and accessible by services in other platforms. To define a data contract, apply the DataContract attribute to the class to serialize the class by a serializer, and apply the DataMember attribute to the fields in the class that must be serialized. A StudentInformation data contract can be defined as follows:
[DataContract]
public class StudentInformation
{
}
Data Member
A data member specifies the type which is part of a data contract used as a composite type member of the contract. To define a data member, apply the DataMember attribute to the fields that must be serialized. The DataMember attribute can be applied to private properties, but they will be serialized and deserialized, and will be accessible to the user or process. The code below shows how to define a data member in a data contract:
[DataContract] public class StudentInformation { _studentId = studId; [DataMember] public int StudentId { get { return _studentId; } set { _studentId = value; } } }
Hope you get this examples