In WCF, ServiceContract, OperationContract, and
DataContract serve distinct purposes in defining how a service operates and communicates with clients. Here's a clear breakdown:
1. ServiceContract
Purpose: Defines the interface (contract) for a WCF service — i.e.,
what operations the service exposes to clients.
Applied To: An interface or class.
Example:
[ServiceContract]
public interface ICalculator
{
[OperationContract]
int Add(int x, int y);
}
2. OperationContract
Purpose: Marks individual methods in the service contract that are exposed as
operations to clients.
Applied To: Methods inside a ServiceContract.
Example:
[OperationContract]
int Subtract(int x, int y);
Only methods marked with OperationContract are accessible to clients.
3. DataContract
Purpose: Defines the structure of complex data types (objects) that are passed in and out of service operations.
Applied To: Classes or structs used as parameters or return types.
Example:
[DataContract]
public class Person
{
[DataMember]
public string Name { get; set; }
[DataMember]
public int Age { get; set; }
}
Only members marked with DataMember are serialized for transport.
Summary Table
Attribute
Applied To
Purpose
ServiceContract
Interface/class
Defines the service and its public interface
OperationContract
Method
Marks the method as a callable operation
DataContract
Class/struct
Defines complex data structures
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In WCF,
ServiceContract,OperationContract, andDataContractserve distinct purposes in defining how a service operates and communicates with clients. Here's a clear breakdown:1. ServiceContract
Example:
2. OperationContract
ServiceContract.Example:
Only methods marked with
OperationContractare accessible to clients.3. DataContract
Example:
Only members marked with
DataMemberare serialized for transport.Summary Table
ServiceContractOperationContractDataContract