what is the difference between class without DataContract attributes:
public class BankOperationResult
{
public int CurrentAmount { get; set; }
public bool Success { get; set; }
}
and the same class with DataContract attributes:
[DataContract]
public class BankOperationResult
{
[DataMember]
public int CurrentAmount { get; set; }
[DataMember]
public bool Success { get; set; }
}
I mean, does WCF treats those two types in different way when encoding etc.?
With or without those attributes my WCF service works...
Thanks, Pawel
A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged. That is, to communicate, the client and the service do not have to share the same types, only the same data contracts.
[DataContract] attribute specifies the data, which is to serialize (in short conversion of structured data into some format like Binary, XML etc.) and deserialize(opposite of serialization) in order to exchange between the client and the Service.
Data Member are the fields or properties of your Data Contract class. You must specify [DataMember] attribute on the property or the field of your Data Contract class to identify it as a Data Member. DataContractSerializer will serialize only those members, which are annotated by [DataMemeber] attribute.
Remarks. Apply the DataMemberAttribute attribute in conjunction with the DataContractAttribute to identify members of a type that are part of a data contract. One of the serializers that can serialize data contracts is the DataContractSerializer. The data contract model is an "opt-in" model.
Before .NET 3.5 SP1 if you didn't mark your property with a DataMember attribute it was not exposed in the WSDL and not serialized. Starting from .NET 3.5 SP1 the DataContractSerializer will automatically include all public properties, so you no longer need to decorate them with this attribute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With