Contracts in WCF
In WCF, all services expose contracts. The contract is a
platform-neutral and standard way of describing what the service does. In this
blog I will told you that which type of contracts available in WCF and how we
can use them. This blog is only provides an introductory knowledge in WCF
WCF defines four types of contracts.
Service contracts define which type of operations the client
can perform on the service. We used two types of attributes to define service
ServiceContract - This attribute is used to
define the Interface.
OperationContract - This attribute is used to
define the method inside Interface.
Example of service contracts
class Service : IServiceContract
public string HelloWorld(
Data contracts define which data types are passed to and
from the service. WCF defines implicit contracts for built-in types such as int
and string, but we can easily define explicit opt-in data contracts for custom
There are two types of Data Contracts.
DataContract –DataContract attribute used to define the class
DataMember –DataMember attribute used to define the properties.
Example of Data contracts
public string Name;
public string City;
If DataMember attributes are not specified for a properties
in the class that property can't be passed to-from web service.
Define which errors are raised by the service, and how the
service handles and propagates errors to its clients.
Allow the service to interact directly with messages.
Message contracts can be typed or untyped, and are useful in interoperability
cases and when there is an existing message format we have to comply with.