C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » WCF/Webservices »

Contracts : An overview


Posted Date: 03 Jul 2009    Resource Type: Articles    Category: WCF/Webservices
Author: Gaurav AroraMember Level: Diamond    
Rating: 4 out of 54 out of 54 out of 54 out of 5Points: 35



In simple words contract defines the functionality provides/offers by a service and functionaly uses by the client.

Main point to note is : Contract can be completely independent of the implementation of service.

In WCF[Windows Comunication Foundation] can be grouped in following three different contract types:

  • Data Contract
    Describes a data structure.Maps CLR types to XSD. In other words we can say that it defines the Data received by and returned from Service.

    Here CLR types are mapped to XML schemas. Data Contract requires explicit marking of the fields that should be serialized with the [DataMember] attribute. [DataMember] attribute can be used regardless wheteher the field is private or public.

    Data contract is different from .NET Serialization like :

    runtime serialization: all fields are serialized including private fields.This mechanism is used by Remoting.
    XML serialization: only public fields and properties are serialized. This mechanism is used by Web Services.

    Following is the piece of example:

    [DataContract (Namespace="http://www.msdotnetheaven.com/SampleServices/MsDnH/2009")]
    public class MsDnHService
    {
    [DataMember]
    public string ServiceId {get; set;}
    }


  • Service Contract
    Describes the operations a service can perform. Maps CLR types to WSDL. Also defines as,Service contract is used to define the WSDL that describes the service. This contract is defined with interfaces or classes with [ServiceContract] attributes and [OperationContract] attributes for method offered by the service. Here is an example:


    [ServiceContract]
    public interface IMsDnHService
    {
    [OperationContract]
    bool StartServices(MsDnHService msdnhService);
    }


  • Message Contract
    Defines the structure of the message on the wire. Maps CLR types to SOAP messages.
    It is used when complete control over the SOAP message is needed. With this one can specify that what part of the message should go into the SOAP header and what belongs in the SOAP body.[MessageContract] attribute is used to specify the Message Contract'. The header and the Body of the SOAP message are specified with the attributes [MessageHeader] and [MessageBodyMember].
    Example:

    [MessageContract]
    public class ProcessMsDnHRequestMessage
    {
    [MessageHeader]
    public int requiestId;

    [MessageBodyMember(Position=0) ]
    public MsDnHRequest msdnhRequest;
    }


    Now, above is used with the service contract as shown bellow:

    [ServiceContract]
    public interface IMsDnHRequest
    {
    [OperationContract]
    public MsDnHResponseMessage msdnhRequest (ProcessMsDnHRequestMessage message);
    }




  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    WCF Services  .  Service Contract  .  Message Contract  .  Gaurav  .  G_arora  .  Data Contract  .  Contracts  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Debugging a WCF Service : A detailed view
    Previous Resource: Basic concept of WCF
    Return to Discussion Resource Index
    Post New Resource
    Category: WCF/Webservices


    Post resources and earn money!
     
    Related Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use