Contract in WCF Service

What are contract in WCF Services?

Contract defined the service and its operation. It’s the agreement between the client and WCF Service application between there communication. Contract defined that what the WCF service does.

Service Contract: Service contract define the operation that service provide for client. Service Contract describes the client –callable operations. Its maps the interface and method of your WCF Service to a platform independent form.

For creating the WCF Contract you need to create the interface and there method. You also need to define the service contract and operation contract attribute on interface and there method.

[ServiceContract()]

    public class Service1 : IService1

    {

        [OperationContract()]

        public string GetData(int value)

        {

            return string.Format(“You entered: {0}”, value);

        }

        [OperationContract()]

        public CompositeType GetDataUsingDataContract(CompositeType composite)

        {

            if (composite == null)

            {

                throw new ArgumentNullException(“composite”);

            }

            if (composite.BoolValue)

            {

                composite.StringValue += “Suffix”;

            }

            return composite;

        }

    }

 

Data Contract: Data Contract is for data exchanging between the client and service communication.

Its define the implicit and explicit data types in data contract. Data Contract define the structure and data types for exchanging the service message. Data Contract also define the serialization of data that will be exchange in WCF service.

 

For creating the data contract you need to define the DataContract and DataMember Attribute.

[DataContract]

    public class CompositeType

    {

        bool boolValue = true;

        string stringValue = “Hello “;

 

        [DataMember]

        public bool BoolValue

        {

            get { return boolValue; }

            set { boolValue = value; }

        }

 

        [DataMember]

        public string StringValue

        {

            get { return stringValue; }

            set { stringValue = value; }

        }

    }

 

Message Contract: Message contract define the SOAP message of the WCF service. SOAP contain the message envelope, Header and Body parts.

You can create the message contact using the MessageContract, MessageHeader and MessageBodyMember attributes in WCF Services.

 

    [MessageContract]

    public class Employee

    {

        [MessageHeader]

        public string EmployeeID;

        [MessageBodyMember]

        public string EmployeeName;

        [MessageBodyMember]

        public string EmployeeLocation;

    }

Fault Contract:

The Fault Contact define the internal exception detail of WCf Service.You can trace the inner exception detail of WCF service using the Fault Exception.

[ServiceContract]

    public interface IService1

    {

 

        [OperationContract]

        [FaultContract(typeof(CustomExceptionClass))]

        string GetData(int value);

 

        [OperationContract]

        [FaultContract(typeof(CustomExceptionClass))]

        CompositeType GetDataUsingDataContract(CompositeType composite);

 

        // TODO: Add your service operations here

    }

Leave a Reply

Your email address will not be published. Required fields are marked *

1 + 9 =


All Rights Reserved 2019 | DesignzRush (OPC) Pvt. Ltd.
Inline
Inline