in Education by

What are Contracts in WCF? How many Contracts are in WPF?

1 Answer

0 votes
by

 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.

contracts in WCF

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.

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:

  1. public interface IService1  
  2. {  
  3.     [OperationContract]  
  4.     string GetData(int value);  
  5.     [OperationContract]  
  6.     CompositeType GetDataUsingDataContract(CompositeType composite);  
  7. }  

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:

DataContract

  1. public class Student  
  2. {  
  3.     private string _Name;  
  4.     private string _City;  
  5.     [DataMember]  
  6.     public string Name  
  7.     {  
  8.         get  
  9.         {  
  10.             return _Name;  
  11.         }  
  12.         set  
  13.         {  
  14.             _Name = value;  
  15.         }  
  16.     }  
  17. }  

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.

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:

  1. [ServiceContract]  
  2. public interface IGetDetailsService  
  3. {  
  4.     [OperationContract]  
  5.     [FaultContract(typeof (Student))]  
  6.     Student GetDetails(string Name);  
  7. }  
  8. [DataContract]  
  9. public class Student  
  10. {  
  11.     private string _Name;  
  12.     private string _City;  
  13.     [DataMember]  
  14.     public string Name  
  15.     {  
  16.         get  
  17.         {  
  18.             return _Name;  
  19.         }  
  20.         set  
  21.         {  
  22.             _Name = value;  
  23.         }  
  24.     }  
  25.     [DataMember]  
  26.     public string City  
  27.     {  
  28.         get  
  29.         {  
  30.             return _City;  
  31.         }  
  32.         set  
  33.         {  
  34.             _City = value;  
  35.         }  
  36.     }  
  37. }

Related questions

0 votes
    What is Security Implementation in WCF? How many are there?...
asked Apr 3, 2021 in Education by JackTerrance
0 votes
    What is WCF Concurrency and How many modes are of Concurrency in WCF?...
asked Apr 2, 2021 in Education by JackTerrance
0 votes
    How to create Basic HTTP Binding in WCF?...
asked Apr 3, 2021 by JackTerrance
0 votes
    What is REST and how to create a WCF RESTful Service ?...
asked Apr 2, 2021 in Education by JackTerrance
0 votes
    What is Transaction Propagation? And how WCF support it?...
asked Apr 2, 2021 in Education by JackTerrance
0 votes
    How do we host a WCF service in IIS?...
asked Apr 2, 2021 in Education by JackTerrance
0 votes
    What is Exception Handling in WCF? What are the ways for WCF Exception Handling?...
asked Apr 2, 2021 in Education by JackTerrance
0 votes
    What are the main components of WCF Service?...
asked Apr 2, 2021 in Education by JackTerrance
0 votes
    What are the possible ways of hosting a WCF service?...
asked Apr 2, 2021 in Education by JackTerrance
0 votes
    What is Binding in WCF?What are the types of binding?...
asked Apr 2, 2021 in Education by JackTerrance
0 votes
    What is Information cards in WCF?...
asked Apr 3, 2021 in Education by JackTerrance
0 votes
    What is Instance Management in WCF?...
asked Apr 3, 2021 in Education by JackTerrance
0 votes
    What you understand by Fault Exception in WCF?...
asked Apr 3, 2021 in Education by JackTerrance
0 votes
    What is Duplex in WCF? Explain also Message Exchange Pattern?...
asked Apr 3, 2021 in Education by JackTerrance
0 votes
    What is WCF Messaging Layer?...
asked Apr 3, 2021 in Education by JackTerrance
...