Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF and the [DataMember] Attribute

Tags:

c#

wcf

I have the following (abbreviated) class that is sent and received to/from the client via WCF:

public class Sparetime : ChartConfigurationBase, IChartConfiguration
{
    [DataMember]
    public int SparetimeConfigurationId { get; set; }

    public Single FeederOffRate { get; set; }
}

Notice the first property uses the DataMember attribute and the second doesn't. Am I correct that only the first property would get serialized and sent to the client when a WCF call is made?

like image 461
Randy Minder Avatar asked Dec 03 '25 17:12

Randy Minder


1 Answers

Yes, you are right, the MSDN documentation specifies it :

When applied to the member of a type, specifies that the member is part of a data contract and is serializable by the DataContractSerializer.

You should add DataContract attribute to your class to make it serializable :

[DataContract]
public class Sparetime : ChartConfigurationBase, IChartConfiguration
{
}

Note that FeederOffRate will be set to its default value (null for reference types).

like image 110
AlexH Avatar answered Dec 06 '25 08:12

AlexH