WCF - Data Contract - Day 4

Before reading this article, please go through the following articles:
  1. Day 1 - WCF Introduction and Contracts
  2. Day 2: WCF Fault Contacts
  3. Day 3: WCF Message Exchange Patterns

DataContract

 
Data contracts are the conceptual agreement of the format and structure of the data in the message exchanged between a service and client. It is a format of data passed to and from the service operations. It defines the structure and types of data exchanged in service messages.
 

DataContractAttribute

 
The DataContractAttribute is defined in the System.Runtime.Serialization namespace. It is used to declare a type as a DataContract.
 
Properties of DataContractAttribute
 
DataContractAttribute in WCF 
  • Name - It determines the type name as it is generated in the resulting schema.
  • Namespace - It sets the target namespace of the schema.

DataMemberAttribute

 
The DataMemberAttribute is also the part of the System.Runtime.Serialization namespace. It is applied to the members and it is used to declare that the members should be included in the serialization.
 
Properties of DataMemberAttribute
 
DataMemberAttribute in WCF 
  • EmitDefaultValue - If you want to add default values in the serialization then set the EmitDefaultValue to true else EmitDefaultValue to false. By default it is true.
  • IsRequired - It controls the minOccurs attribute for the schema element. The default value is false.
  • Name - It creates the schema element name generated for the member.
  • Order - It maintains the order of each element in the schema. By default it appears alphabetically.
Note
  • For example, you set the EmitDefaultValue to false and IsRequired to true and you are not assigning any value, at that time it creates a problem. Because the serializer emits the default value and we are not passing any value, on the other side it is required. That is why it throws an error.
  • If you apply a data member attribute to both property and field, then it generates duplicate members in the schema.
Sample Code
  • Create one DataContract for Place. Add the data members PlaceCode and PlaceName.
  • Set the order for the data members and set IsRequired to true for PlaceCode.
  • Create one OperationContract with return type Place.
    1. namespace DataContractServiceApp  
    2. {  
    3.     [ServiceContract]  
    4.     public interface IService1  
    5.     {  
    6.         [OperationContract]  
    7.         Place GetPlace(string PlaceCode);  
    8.     }  
    9.    
    10.     [DataContract(Name ="Place", Namespace ="")]  
    11.     public classPlace  
    12.     {  
    13.         [DataMember(Name ="PlaceCode", Order = 1, IsRequired=true)]  
    14.         public string PlaceCode;  
    15.         [DataMember(Name ="PlaceName", Order = 2)]  
    16.         public string PlaceName;  
    17.     }  
    18. } 
  • In the service class, the GetPlace method is implemented and in that we create an object of place, assign some values to it and return a Place object.
    1. namespace DataContractServiceApp  
    2. {  
    3.     public class Service1 : IService1  
    4.     {  
    5.         public Place GetPlace(string PlaceCode)  
    6.         {  
    7.             Place place = new Place();  
    8.             // Get placename by placecode from database.  
    9.             return place;  
    10.         }  
    11.     }  
    12. }


Similar Articles