Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataContract & DataMember attributes - how they affect type

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

like image 438
dragonfly Avatar asked Nov 27 '09 22:11

dragonfly


People also ask

What is Datacontract?

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.

What is Datacontract attribute in C#?

[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.

What does DataMember mean?

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.

What does DataMember attribute do?

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.


1 Answers

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.

like image 91
Darin Dimitrov Avatar answered Nov 09 '22 11:11

Darin Dimitrov