What are contracts in WCF?
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.
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.
string MyMethod( );
class MyService : IMyContract
public string MyMethod( )
return "Hello World";
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.
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.
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.
Policy and Binding
Specifies the transport media such as HTTP or TCP. It aslo includes some of the securoty requirements. which are necessary for Commucation.
What is the address formats of the WCF transport schemas?
Address format of WCF transport schema always follow
[transport]://[machine or domain][:optional port] format.
HTTP Address Format
the way to read the above url is
"Using HTTP, go to the machine called localhost, where on port 8888 someone is waiting"
When the port number is not specified, the default port is 80.
TCP Address Format
When a port number is not specified, the default port is 808:
NOTE: Two HTTP and TCP addresses from the same host can share a port, even on the same machine.
IPC Address Format
We can only open a named pipe once per machine, and therefore it is not possible for two named pipe addresses to share a pipe name on the same machine.
MSMQ Address Format