WCF Contracts.

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 contracts.

WCF defines four types of contracts.

Service contracts

Service contracts define which type of operations the client can perform on the service. We used two types of attributes to define service contract.

ServiceContract - This attribute is used to define the Interface.

OperationContract - This attribute is used to define the method inside Interface.

Example of service contracts


interface IServiceContract



   string MyMethod( );


class Service : IServiceContract


   public string HelloWorld( )


      return 'Hello World';



Data contracts

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 types.

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


class ServiceContract



   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.

Fault contracts

Define which errors are raised by the service, and how the service handles and propagates errors to its clients.

Message contracts

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.

Last updated:9/18/2014 1:23:58 PM


Leave Comment