Understanding Various Types Of Contracts in WCF

Understanding Various Types Of Contracts in WCF

This article will explain various types of contracts, and how these contracts can be used in a real life application. WCF defines different types of contracts – Data Contract, Service Contract, Operation contract, Message Contract, Fault contract. Click here to know more about different types of Bindings in WCF.


Data Contract

 A data contract contains information about the data to be exchanged between service and client. It generally uses “System.Runtime.Serialization“ to serialize the data. Data contracts can be defined by annotating a class, enumeration, structure, but not an interface.  A data contract can be used by an operation contract as a parameter or return type, or it can be used by a message contract to define elements.

[DataContract]

     public class Employee

    {

        [DataMember]

        public int EmployeeId { get; set; }

        [DataMember]

        public string Name { get; set; }

 

        [DataMember]

        public bool Result { get; set; }

 

        [DataMember]

        public string ErrorMessage { get; set; }

 

        [DataMember]

        public string ErrorDetails { get; set; }

    }

 

 

[ServiceContract]

    public interface IEmployeeService

    {

        [OperationContract]

        string GetEmployeeName(int employeeId);

 

        [OperationContract]

        Employee GetEmployee(int employeeId);

     }



Service Contract

 A service contract defines the operations or methods which are exposed by the service to the outside world. A service contract is the interface of the WCF service and uses to configure on the service endpoint. Service contract describes the message exchange pattern can have with client. Might be One-Way/request/reply/duplex.

[ServiceContract]

    public interface IEmployeeService

    {

        [OperationContract]

        string GetEmployeeName(int employeeId);

 

        [OperationContract]

        Employee GetEmployee(int employeeId);

    }


Service contact need to be configured in the contact attribute of the endpoint in the .config file

<endpoint address="" binding="basicHttpBinding" contract="WcfServiceLibrary1.IEmployeeService">

Operation Contract

An operation contract is defined within a service contract. It defines the methods of the service that are accessible by external systems. It defines the parameters and return type of an operation. An operation contract can also defines operation-level settings, like transaction flow of the operation, the directions of the operation (one-way, two-way, or both ways) and fault contract of the operation.

[OperationContract]

        string GetEmployeeName(int employeeId);

 

        [OperationContract]

        Employee GetEmployee(int employeeId);


Method Overloading in Operation contract

WCF provides the ability to overload methods.  This can be achieved by using the name property of the OperationContract attribute.

[OperationContract (Name="GetEmployeeById")]

 Employee GetEmployee(int employeeId);

 

 [OperationContract(Name = "GetEmployeeByIdName")]

 Employee GetEmployee(int employeeId, string name);


Message Contract

WCF uses SOAP for communication. Message contract control over the SOAP message format, Message can be customized based on the requirement. A Message Contract is used to control the structure of a message body and serialization process. It is also used to send / access information in SOAP headers. A message is nothing but the packet and WCF uses this packet to transform information from service to client.

Rules to follow for Message Contract

    When using Message contract type as parameter, only one parameter can be used in Operation.

    Service operation either should return Message Contract type or it should not return any value.

    Operation will accept and return only message contract type. Other data types are not allowed.

namespace WcfServiceLibrary1

{

    [ServiceContract]

    public interface IEmployeeService

    {

        [OperationContract]

        [FaultContract(typeof(Employee))]

        EmployeeResponse GetInfo(EmployeeRequest Req); 

 

    }

    [MessageContract]

        public class EmployeeRequest

    {

        [MessageHeader]

        public string AutherId;

    }

 

    [MessageContract]

    public class EmployeeResponse

    {

        [MessageBodyMember]

        public Employee Obj;

    }

 

  

    [DataContract]

    public class Employee

    {

        [DataMember]

        public int EmployeeId { get; set; }

        [DataMember]

        public string Name { get; set; }

        [DataMember]

        public bool Result { get; set; }

        [DataMember]

        public string ErrorMessage { get; set; }

        [DataMember]

        public string ErrorDetails { get; set; }

    }

}



Fault Contract

Fault Contract uses to handle errors accorded in the service to client. This helps as to easy identity what and where error has occurred. An operation contract can have zero or more fault contracts associated with it.

[ServiceContract]

    public interface IEmployeeService

    {

        [OperationContract]

        [FaultContract(typeof(Employee))]

        string GetEmployeeName(int employeeId);

 

    }

[DataContract]

    public class Employee

    {

        [DataMember]

        public int EmployeeId { get; set; }

        [DataMember]

        public string Name { get; set; }

        [DataMember]

        public bool Result { get; set; }

        [DataMember]

        public string ErrorMessage { get; set; }

        [DataMember]

        public string ErrorDetails { get; set; }

    }


Summary

I hope this article will help you to understand diifferent types of contracts in WCF. Please share your valuable feedback on this.
comments powered by Disqus

Related Articles