Contracts in WCF Service

Contracts are useful to built the WCF service application .Contracts defines what protocol (binding) does service use ,how communication will be done ,what message exchange format to use and much.
The following are the Contracts which are available in WCF
  • Service contracts
  • Data contracts
  • Message contracts
  • Fault Contract 
  • Operation contract
Service contracts
Service contracts nothing but the it defines the Interface for the service.It can be defined as follows
Syntax
   [ServiceContract]
    public interface IService1
    {

        // TODO: Add your service operations here
    }
 
Key Points 
  • Any WCF Service can have More than one service contract. 
  • It must to declare at least one service contract in a service.
  •  Service contract can be declared by using [ServiceContract] attribute. 
  • It allows defining operation contract under it to expose service outside the world. 
  • It maps the interface and methods of your service to a platform-independent description.
  •  It describes message exchange patterns that the service can have with another party. Some service operations might be one-way; others might require a request-reply pattern. 
Service contract attribute has following properties
  • CallbackContract 
  • ConfigurationName 
  • HasProtectionLevel 
  • Name 
  • Namespace 
  • ProtectionLevel
  • SessionMode
  • TypeId
Operation contract
Operation contract defines the method which is exposed to the client to exchange the information between client and server.Operation contract describes the what functionality to be given to the client such as Addition, subtraction etc
It can be defined as
   public interface IService1
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
  
    }
Operation contract attribute has following properties
CallbackContract :Gets or sets the type of callback contract when the contract is a duplex contract.
ConfigurationName :Gets or sets the name used to locate the service in an application configuration file.
HasProtectionLevel : Gets a value that indicates whether the member has a protection level assigned.
Name : Gets or sets the name for the <portType> element in Web Services Description Language (WSDL).
Namespace : Gets or sets the namespace of the <portType> element in Web Services Description Language (WSDL).
ProtectionLevel : Specifies whether the binding for the contract must support the value of the ProtectionLevel property.
SessionMode : Gets or sets whether sessions are allowed, not allowed or required.
TypeId: When implemented in a derived class, gets a unique identifier for this Attribute.
Data Contract
Data Contracts defines the data type for variables which are  are same as get set properties but difference is that data contract in WCF is used to serialize and deserialize the complex data.it defines how data types are serialized and deserialized. Through serialization, you convert an object into a sequence of bytes that can be transmitted over a network. Through De-serialization, you reassemble an object from a sequence of bytes that you receive from a calling application.
It can be defined as follows 
    [DataContract]
    public class Student
    {
        private string _Name;

        private string _City;


        [DataMember]
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
}
Fault Contract
Fault contract is used to handle the exception and know the cause of error which is occur in WCF service, basically when we develop managed application or service, we will handle the exception using try- catch block. But these exceptions handling are technology specific.
In order to support interoperability and client will also be interested only, what went wrong? Not on how and where cause the error.
The following is the syntax to raise the custom error in WCF
[ServiceContract]
    public interface IGetDetailsService
    {
        [OperationContract]
        [FaultContract(typeof(Student))]
        Student GetDetails(string Name);
    }

    [DataContract]
    public class Student
    {
        private string _Name;

        private string _City;

        [DataMember]
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        [DataMember]
        public string City
        {
            get { return _City; }
            set { _City = value; }
         }
}
Message contracts
Default SOAP message format is provided by the WCF run time for communication between Client and service. If it is not meeting your requirements then we can create our own message format. This can be achieved by using Message Contract attribute.
It can be defined as
[MessageContract]
public class Person
{
  [MessageHeader] public Operation Name;
  [MessageHeader] public string city;
  [MessageBodyMember] private Home Address;
  [MessageBodyMember] private Home Streat;
  [MessageBodyMember] public int age;
}
Note
Please refer my following articles for complete understanding
Summary
I hope this article is useful for all students and beginners. If you have any suggestion related to this article then please contact me.

Post a Comment

www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode