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.
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
}
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
}
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
}
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.
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; }
}