DataContract & DataMember attributes - how they affect type
Asked Answered
C

1

6

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

Covalence answered 27/11, 2009 at 22:6 Comment(0)
T
10

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.

Terr answered 27/11, 2009 at 22:15 Comment(1)
Unfortunately, it's not quite that simple in the 3.5 SP1 case, but that's the basic idea. Without any DataContract or DataMember attributes defined on your class, all fields and properties will be serialized into XML and this includes the private auto-generated backing store fields for automatic properties of the form public int MyValue { get; set; } and will name them <MyValue>k__BackingField. Adding the DataContract and DataMember attributes explicitly will have the DataContractSerializer forego the private field serialization.Therrien

© 2022 - 2024 — McMap. All rights reserved.