Google News
logo
WCF - Interview Questions
What are Contracts in WCF? How many Contracts are in WPF?
The main use of contracts is to allow the client and services agree as to the types of operations and structures they will use during the communication process. It also shows the formal agreements between client and service to define a platform-neutral and standard for describing that what the service does.
 
Service Contract : A service contract defines the operations which are exposed by the service to the outside world. A service contract is the interface of the WCF service and it tells the outside world what the service can do. It may have service-level settings, such as the name of the service and namespace for the service.
[ServiceContract]
interface IMyContract
{
[OperationContract]
string MyMethod();
}

class MyService : IMyContract
{
public string MyMethod()
{
return "Namaste";
}​

Operation Contract :
An Operation Contract defines the method exposed to the client to exchange the information between the client and server. An Operation Contract describes what functionality is to be given to the client, such as addition, subtraction and so on.
 
It can be defined as in the following :
public interface IService1
{
    [OperationContract]
    string GetData(int value);
    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);
}
 

Data Contract :
Data Contracts define the data type for variables that are the same as get and set properties but the difference is that a Data Contract in WCF is used to serialize and deserialize the complex data. It defines how data types are serialized and deserialized. Using serialization, you can convert an object into a sequence of bytes that can be transmitted over a network. Using de-serialization, you reassemble an object from a sequence of bytes that you receive from a calling application.
 
Example :
public class Student
{
    private string _Name;
    private string _City;
    [DataMember]
    public string Name
    {
        get
        {
            return _Name;
        }
        set
        {
            _Name = value;
        }
    }
}

Message Contract :
When an operation contract required to pass a message as a parameter or return value as a message, the type of this message will be defined as message contract. A message contract defines the elements of the message (like as Message Header, Message Body), as well as the message-related settings, such as the level of message security.

The SOAP header is implemented in the namespace system.web.services.protocol.  
[MessageContract]
public class AutherRequest
{
   public string AutherId;
}
Message Header : A Message Header is applied to a member of a message contract to declare the member within the message header.
[MessageContract]
public class AutherRequest
{
   [MessageHeader]
   public string AutherId;
}

 

Message Body Member :  A Message Body Member is applied to the member of a message contract to declare the members within the message body.
[MessageContract]
public class AuthorResponse
{
    [MessageBodyMember]
    public Auther Obj;
}

Fault Contract :
A fault contract defines errors raised by the service, and how the service handles and propagates errors to its clients. An operation contract can have zero or more fault contracts associated with it.
 
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;
        }
    }
}
Advertisement