WCF Data Contract Attributes

This article will give you the details of wcf data contract attributes like Name, Namespace. And difference between DataContractSerilizer and XMLSerilizer.

Data contract is included in System.Runtime.Serilization. It defines the structure of data which flows from client to service and vice versa and hence it is called as structural contract. It can be applied to class, struct or enum. Data contracts are similar to XSD of XML.


WCF data contract has below attributes.


  1. Name

    Name is string property which allows you to give meaningful name to your data contract. The default value for Name attribute is the name of class. One of the possible use of Name property is to set it with value which is not allowed as Type name for example the "Order#" is not allowed as Type name but you can set it as value for Name attribute of data contract.

                    
        [DataContract(Name = "CustomerOrders")
        public class Order
        {
            // data members
        }
                        
                
  2. Namespace

    Namespace is string property with default value like http://schemas.datacontract.org/2004/07/<yourservicenamespace>. You can change the namespace as per your requirements. Ideally the data contract namespace should be similar with ServiceContract namespace and should be set to bindingNamespace of corresponding endpoint.

                    
        [DataContract(Namespace = "http://Northwind.com/services/order")]
        public class Order
        {
            // Data members for Orders
        }
                

    Setting namespace attribute explicitly can help you for data contract versioning. Data contracts from below example will be included in WSDL as different properties and client can use any or both versions

                    
        [DataContract(Namespace = "http://Northwind.com/services/v1/order")]
        public class Order
        {
            // Version 1 data members
        }
    
        [DataContract(Namespace = "http://Northwind.com/services/v2/order")]
        public class Order
        {
            //Version 2 data members
        }
                
  3. IsReference

    IsReference is a Boolean attribute with default value as false of WCF DataContract. If set it as a True the object reference data will be preserve means DataContract can serialize an object graph that contains circular references. It is introduced in .Net 3.5 SP1 and Net 3.0 SP2

                    
          [DataContract(IsReference=true]
          public class Order
          {
            // data members
          }
                        
                

Data Contract Serializer

DataContractSerializer is sealed class and inherited from XmlObjectSerializer, included in System.Runtime.Serialization namespace. It serializes and deserializes data contracts or data types into XML. Windows Communication Foundation by default uses DataContractSerializer for serialization and deserialization.


Difference between DataContractSerializer and XMLSerializer

  1. DataContractSerializer uses the opt-in mode to serialize and deserialize data where as XMLSerializer uses opt-out mode.
  2. With DataContractSerializer you have to explicitly mark the fields and properties as [DataMember] to make the type serializable resulting in better performance than XMLSerializer. In XMLSerializer the fields and properties are by default serializable and for making it non serializable you need to mark as [XmlIgnore].
  3. DataContractSerializer can serialize public or private properties however XMLSerializer can serialize only public properties.
  4. DataContractSerializer serializes types which implements IDictionary interface where as XMLSerializer cannot.
  5. XMLSerializer gives you more control than DataContractSerializer on generated XML in terms of attribute and elements.

Below example uses DataContractSerializer by marking class as [DataContract]. Property OrderTotal is not needed to serialize so not marked as [DataMember].

        
    [DataContract]
    public class Order
    {
      [DataMember]
      public int OrderID
      { get; set; }

      [DataMember]
       public string CustomerName
      { get; set; }        
       
       [DataMember]
       public string CustomerAddress
       { get; set; } 

       public double OrderTotal
       { get; set; }

     }
            

Below example uses XMLSerializer by marking class as [Serializable()]. Property OrderTotal is not needed to serialize so marked as [XmlIgnore].

        
    [Serializable()] 
    public class Student
    {   
      public int OrderID
      { get; set; }
     
      public string CustomerName
      { get; set; }
      
      public string CustomerAddress
      { get; set; } 

      [XmlIgnore] 
      public double OrderTotal
      { get; set; }       
    }
            

Speak your mind :
Leave a comment for this article on dotnetbloogers.com