Wednesday, December 12, 2012

WCF Contarcts

 A contract is a agreement between two or more parties for common understanding and it is a is a platform-neutral and standard way of describing what the service does. In WCF, all services expose contracts.
1) Operation Contract: An operation contract defines the parameters and return type of an operation.
[OperationContract]
double Add(double i, double j);

2) Service Contract: Ties together multiple related operations contracts into a single functional unit.
[ServiceContract] //System.ServiceModel
public interface IMath
{
[OperationContract]
double Add(double i, double j);
[OperationContract]
double Sub(double i, double j);
[OperationContract]
Complex AddComplexNo(Complex i, Complex j);
[OperationContract]
Complex SubComplexNo(Complex i, Complex j);
}
3) Data Contract: The descriptions in metadata of the data types that a service uses.
[DataContract] //using System.Runtime.Serialization
public class Complex
{
private int real;
private int imaginary;
[DataMember]
public int Real { get; set; }
[DataMember]
public int Imaginary { get; set; }
}

6 comments:

  1. In WCF, all services expose contracts. The contract is a platform-neutral and standard way of describing what the service does.

    WCF defines four types of contracts.


    Service contracts

    Describe which operations the client can perform on the service.
    There are main two types of Service Contracts.
    ServiceContract - This attribute is used to define the Interface.
    OperationContract - This attribute is used to define the method inside Interface.

    ReplyDelete
  2. [ServiceContract]
    interface IMyContract
    {
    [OperationContract]
    string MyMethod( );
    }
    class MyService : IMyContract
    {
    public string MyMethod( )
    {
    return "Hello World";
    }
    }

    ReplyDelete
  3. Data contracts

    Define which data types are passed to and from the service. WCF defines implicit contracts for built-in types such as int and string, but we can easily define explicit opt-in data contracts for custom types.

    There are two types of Data Contracts.
    DataContract - attribute used to define the class
    DataMember - attribute used to define the properties.

    [DataContract]
    class Contact
    {
    [DataMember]
    public string FirstName;

    [DataMember]
    public string LastName;
    }

    ReplyDelete
  4. If DataMember attributes are not specified for a properties in the class, that property can't be passed to-from web service.

    ReplyDelete
  5. Fault contracts

    Define which errors are raised by the service, and how the service handles and propagates errors to its clients.

    ReplyDelete
  6. Message contracts

    Allow the service to interact directly with messages. Message contracts can be typed or untyped, and are useful in interoperability cases and when there is an existing message format we have to comply with.

    ReplyDelete