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.

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

WCF data contract has the below attributes.

  1. Name

    Name is a string property of DataContract that allows you to give a meaningful name to your data contract. The default value for the Name attribute is the name of the class. One of the possible use of Name property is to set it with a 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 the Name attribute of data contract.

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

    The Namespace is a string property of DataContract with a 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 to the ServiceContract namespace and should be set to bindingNamespace of the corresponding endpoint.

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

    Setting the namespace attribute explicitly can help you with data contract versioning. Data contracts from the below example will be included in WSDL as different properties and the 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 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 a 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 whereas 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 it as [XmlIgnore].
  3. DataContractSerializer can serialize public or private properties however XMLSerializer can serialize only public properties.
  4. DataContractSerializer serializes types that implement IDictionary interface whereas XMLSerializer cannot.
  5. XMLSerializer gives you more control than DataContractSerializer on generated XML in terms of attributes 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
Please login to post your comment!


Blog Search





If you like my content please feel free to buy me coffee. Buy Me A Coffee