Pages

Thursday, June 27, 2013

Contracts in WCF

The contracts are very important in WCF which are used to connect with the client.   

The following types of contracts are available in WCF
·         Service Contract
·         Operation Contract
·         Message Contract
·         Data Contract
·         Fault Contract

Service Contract
The service contract is the service which we will be exposing to the client .By accessing this service the client can access the operations available in the service. Basically service contract is a class. Any class which has the tag    “[ServiceContract]” above it will be considered as a service contract as can be seen in the below example.

Example 1:

namespace KSS.WCFService.ServiceContracts
{
    [ServiceContract]
    public partial Class KSSService
    {
        #region KSS 1                            
        [FaultContract(typeof(KSS.WCFService.FaultContracts.UnknownFault))]
        [OperationContract]
        MessageResponse Calculate(MessageRequest Request);                   
        #endregion
    }
}

Message Contract

Message contracts are the rapper classes over the attributes. The WCF communicates using SOAP messages so any data cannot be sent/received between client and server just like that. They have to wrap inside a message contract and then sent to client or server. Basically a message contract is a class. A class having the tag “[MessageContract]” above the class is considered as a Message contract as shown below.

Example2:

 
namespace KSS.WCFService.MessageContracts
{
                [MessageContract]
                public partial class MessageData
                {
                                private KSS.WCF.DataContracts data;
                                private bool isValid;
                                               
                                [MessageBodyMember]
                                public KSS.WCF.DataContracts.Reult Data
                                {
                                                get { return data; }
                                                set { data = value; }
                                }
                                               
                                [MessageBodyMember]
                                public bool IsValid
                                {
                                                get { return isValid; }
                                                set { isValid = value; }
                                }
                }
}

 

A message contract can contain data types like Integer, Strings along with collections, Lists etc. In the above example we can see that message contract has a data contract and a Boolean variable IsValid.

Data Contract

A data contract is the entities used in a normal C# code. Basically it’s a class which has the variables and its properties within it. Any class with the tag “[DataContract]” above it are considered as a data contract class as shown in below example.

Example3:

namespace KSS.WCFService.DataContracts
{
    [DataContract]
    public partial class DCCalculator
    {
                #region Declaring Variables
               
         private int  numberA;
         private int numberB;
         private int sum;
        [DataMember(Name = "ItemA", IsRequired = false, Order = 0)]
        public int NumberA
        {
            get { return numberA; }
            set { numberA = value; }
        }
        [DataMember(Name = "ItemB", IsRequired = false, Order = 1)]
        public int NumberB
        {
            get { return numberB; }
            set { numberB = value; }
        }
                               
        [DataMember(Name = "Result", IsRequired = false, Order = 2)]
        public int Sum
        {
            get { return sum; }
            set { sum = value; }
        }
        #endregion
    }
}
 

 

The members of the data contract class should have “[DataMember]” tag. Only those which have these tags will be visible for the client otherwise the definitions for those will not be available in the Proxy.

Fault Contract

A fault contract is a class which is used to add the fault or exception details and sent to the client if in case an exception has occurred in the services side. Basically it’s class. As can been seen in example 1 the tag “[FaultContract]” is used to define a fault contract.

We will create a WCF Service in the next blog. Hope this was helpful.

No comments:

Post a Comment