Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Data Contract / Serialization

I created a simple WCF application which expose one operation. This operation takes a composite data type as parameter.

I have not decorated this composite data type with [DataContract] attribute. But this is working and I can see the Schema for this in WSDL.

Now my understanding is that this new custom type should be decorated with [Serializable] or [dataContract] attribute to take part in the Web services operation.

What I am missing here?

like image 797
Aakash Avatar asked Dec 03 '22 05:12

Aakash


2 Answers

POCO support have been introduced in WCF since .NET 3.5 SP1 and you no longer need to decorate your objects with [DataContract] and [DataMember] attributes. Public properties will be automatically exposed. Although I would recommend you explicitly marking them with those attributes.

like image 174
Darin Dimitrov Avatar answered Dec 25 '22 23:12

Darin Dimitrov


As Darin says, sp1 introduced support for inferred data contracts. If the marshalled type is a public type and it is not decorated with the DataContract attribute, WCF will automatically infer such an attribute and apply the DataMemeber attribute to all public members of the type.

In his book "Programming WCF Services", Juval Löwy says;

In my opinion, relying on inferred data contracts is a sloppy hack that goes against the grain of most everything else in WCF. ... Do use the DataContract attribute, and be explicit about your data contracts. This will enable you to tap into data contract features such as versioning.

like image 25
Steve Cooper Avatar answered Dec 25 '22 23:12

Steve Cooper