Contracts in WCF

Contracts define certain aspects of the service such as the format and structure of the message, and equally important, the behavior of the service. Service contracts are well-formed XML documents, typically found in the format of WSDL or XSD.
Windows Communication Foundation supports the following three types of contracts:

  • Service Contracts :- Service contracts define the operations that a service will perform when executed. They tell the outside world a lot about the service such as message data types, operation locations, the protocols the client will need in order to communicate with the service, and the operations the service provides.
    In the book publisher example, a service might exist that has a PlaceOrder operation, which accepts order information (name, title, payment type, and so on) and returns success or failure (and if success, an order number). In this scenario the service contract could inform the clients that the service contract contains a PlaceOrder operation and the address of this operation and the type of protocols needed to communicate with this service. As well, the information could include the data types needed for this message.
  • Message Contracts :- Message contracts allow the control of SOAP messages that are produced and consumed by WCF. Basically it boils down to being able to customize the format of contract parameters and the placing of elements within a SOAP message, or in other words, having control of the structure as well as the contents
    of a SOAP message. If you have this need, then this is for you.
    It is highly recommended that if you don’t need this level of control, you should consider using data contracts.
  • Data Contracts :- Data contracts specifically define the data that is being exchanged between a client and service. The data contract is an agreement, meaning that the client and the service must agree on the data contract in order for the exchange of data to take place. Note that they don’t have to agree on the data types, just the contract.
    This is made available due to the fact that all data is serialized to XML and deserialized from XML during the processing of the message. This means that a number of different types can be serialized with no extra preparation. These types include .NET primitive types such as System.Boolean and System.Single, as well as special types (System.DateTime, for example).
    Serialization is provided via a new serialization engine in WCF, called the DataContractSerializer. This engine provides the translation between .NET Framework objects and XML, and vice versa. Serialization occurs at runtime when the message is created and ready to send. WCF selects the serializer based on service contract encoding. The DataContractSerializer is used by default but you can also use the XmlSerializer (currently used in ASMX) by annotating the service contract with an attribute, such as the following:

    [ServiceContract, XmlSerializerFormat]
    public interface IWCFService
    {
        //add Operation Contracts...
    }
Tagged . Bookmark the permalink.

Leave a Reply