Tuesday, 1 May 2012

What are Contracts in WCF | Service Contract | Data Contract | Message Contract | Fault Contract in WCF

                           Hi friends, in this article I will explain about the Contract in WCF.
In previous articles I already explained about Fundamentals of WCF,Binding and behaviour in WCF.
What are Contracts in WCF:
In WCF every service is exposed as contract. Contract is a standard way to describing about what the service will do. A WCF Contract is a collection of Operations that specifies what the Endpoint communicates to the outside world. Each operation is a simple message exchange, for example one-way or request/reply message exchange.
The below figure shows how it will be work 
There are four types of contracts.
  • Service Contract
  • Data Contract
  • Message Contract
  • Fault Contract
Service Contract:
A service contract specifies the following:
·         The operations a contract exposes.
·         The signature of the operations in terms of messages exchanged.
·         The data types of these messages.
·         The location of the operations.
·         The specific protocols and serialization formats that are used to support successful communication with the service.

 It will be created using Service and Operational Contract attribute.

Data Contract:
                             Windows Communication Foundation (WCF) uses a serialization engine called the Data Contract Serializer by default to serialize and deserialize data (convert it to and from XML).
                         Data contract describes the custom data type which is exposed to the client.
A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged.Data contract can be explicit or implicit. Simple type such as int, string etc has an implicit data contract. User defined object are explicit or Complex type, for which you have to define a Data contract using [DataContract] and [DataMember] attribute.

Message Contract:
                       SOAP message format is provided by the WCF runtime. It is the communication between client and service.
If it is not suitable to your requirements then we can write our own message formats.

Fault Contract:
                     Suppose the consumed service is not working in the client application, we want to identify the cause of the problem. For that we are having Fault Contract. Fault Contract provides documented view for error occurred in the service to client. This helps us to easy identity, what error has occurred.

Service Host:
                                  Service host is the process of hosting the registered  End Points and WCF Service. . It loads the service configuration  End Points , apply the settings and start the listeners to handle the incoming request. System.ServiceModel.ServiceHost namespace hold this object. This object is created while self hosting the WCF service.
"If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook page Aspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter  on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on  RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog  for more articles." 

No comments:

Post a Comment

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.