Please enable Javascript to correctly display the contents on Dot Net Tricks!

Understanding different types of WCF Contracts

  Author : Shailendra Chauhan
Posted On : 28 Sep 2013
Total Views : 85,900   
Updated On : 28 Sep 2013
 

WCF contract specify the service and its operations. WCF has five types of contracts: service contract, operation contract, data contract, message contract and fault contract.

  1. 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 "Hello World";
     }
    }
    
  2. Operation Contract

    An operation contract is defined within a service contract. It defines the parameters and return type of an operation. An operation contract can also defines operation-level settings, like as the transaction flow of the op-eration, the directions of the operation (one-way, two-way, or both ways), and fault contract of the operation.

     [ServiceContract]
     interface IMyContract
     {
     [FaultContract(typeof(MyFaultContract))]
     [OperationContract]
     string MyMethod();
     }
    
  3. Data Contract

    A data contract defines the data type of the information that will be exchange be-tween the client and the service. 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]
    class Person
    {
     [DataMember]
     public string ID;
     [DataMember]
     public string Name;
    }
     
    [ServiceContract]
    interface IMyContract
    {
     [OperationContract]
     Person GetPerson(int ID);
    }
    
  4. 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.

    Message contracts give you complete control over the content of the SOAP header, as well as the structure of the SOAP body.

    [ServiceContract]
    public interface IRentalService
    {
     [OperationContract]
     double CalPrice(PriceCalculate request);
    }
    
    [MessageContract]
    public class PriceCalculate
    {
     [MessageHeader]
     public MyHeader SoapHeader { get; set; }
     [MessageBodyMember]
     public PriceCal PriceCalculation { get; set; }
    }
    
    [DataContract]
    public class MyHeader
    {
     [DataMember]
     public string UserID { get; set; }
    }
    
    [DataContract]
    public class PriceCal
    {
     [DataMember]
     public DateTime PickupDateTime { get; set; }
     [DataMember]
     public DateTime ReturnDateTime { get; set; }
     [DataMember]
     public string PickupLocation { get; set; }
     [DataMember]
     public string ReturnLocation { get; set; }
     }
     
  5. 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.

    [ServiceContract]
    interface IMyContract
    {
     [FaultContract(typeof(MyFaultContract1))]
     [FaultContract(typeof(MyFaultContract2))]
     [OperationContract]
     string MyMethod();
     
     [OperationContract]
     string MyShow();
     }
    
What do you think?

I hope you will enjoy the tips while programming with WCF. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.



Free Interview Books
 
COMMENTS (0)
3 APR
Master Class Fast Track MVC 5 with Angular2 Development (Online)

10:30 AM -03:30 PM IST (Fast Track)

Know More
25 MAR
MEAN Stack 2 Development (Classroom)

11:00 AM-12:30 PM

Know More
25 MAR
ASP.NET MVC with Angular2 Development (Classroom)

3:00 PM-4:30 PM

Know More
25 MAR
Xamarin Forms : Build Cross-platform Apps (Classroom)

8:00 AM-9:30AM

Know More
21 MAR
ASP.NET MVC with Angular2 Development (Online)

09:00 PM - 11:00 PM IST( TUS, THR, SAT)

6 MAR
ASP.NET MVC with Angular2 Development (Online)

07:00 AM - 09:00 AM IST( MON, WED, FRI)

25 FEB
ASP.NET MVC with Angular2 Development (Classroom)

05:00 PM-06:30 PM

31 JAN
ASP.NET MVC with Angular2 Development (Online)

07:00 AM-09:00 AM IST(Tuesday & Thursday)

30 JAN
NODEJS & MEAN Stack 2.x Development (Online)

09:00 PM-11:00 PM IST( MON, WED, FRI)

31 DEC
ASP.NET MVC with AngularJS Development (Classroom)

09:30 AM-11:00 AM

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 

Professional Speaks

+