What are contracts in WCF?

 Posted by Poster on 11/13/2008 | Category: WCF Interview questions | Views: 167683

In WCF, all services expose contracts. The contract is a platform-neutral and standard way of describing what the service does.

WCF defines four types of contracts.

Service contracts

Describe which operations the client can perform on the service.
There are two types of Service Contracts.
ServiceContract - This attribute is used to define the Interface.
OperationContract - This attribute is used to define the method inside Interface.


interface IMyContract
string MyMethod( );
class MyService : IMyContract
public string MyMethod( )
return "Hello World";

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 - attribute used to define the class
DataMember - attribute used to define the properties.


class Contact
public string FirstName;

public string LastName;

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.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Rajeshatkiit on: 12/17/2015 | Points: 10
Good simple explanation.

Login to post response