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 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
string MyMethod( );
class Service : IServiceContract
public string HelloWorld( )
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 –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.