WCF datacontract vs class serialize

I understand that we can have more controls for the class if we use datacontract, however, consider the following 2 cases

[DataContract] public class Customer { [DataMember] public string CustomerName {get; set;} [DataMember] public int Age{get; set;} } 

and

 public class Customer { public string CustomerName {get; set;} public int Age{get; set;} } 

They both serialize correctly on the .net client. And personally, I do not use the second example. Can someone point me to the differences in the 2 classes? I wanted to send all public properties to both classes.

+7
source share
2 answers

The second version is the contract version with POCO (plain old CLR) and can be used with WCF with 3.5 sp1.

I would not recommend using it, as it gives you very little control over serialization (namespace attributes ...) and associates your office objects with your business objects (which may be the same with POCO)

+5
source

Anyway, here is the best story from WCF Service Programming 3rd Edition

When using the Serializable attribute is workable, it is not ideal for service-oriented interaction between customers and services. Rather than labeling all members of a type as serializable and therefore part of the data for that type, it would be preferable to have a selection approach where only the parties to the contract the developer wants to explicitly include in the data contract. The serializable attribute forces the data type to be serialized to be used as a parameter in the contract and it does not offer a clean separation between the ability to use the type as a parameter of the WCF (aspect of the "service" type) and the possibility of its serialization. the attribute does not support names or members of the alias type or for matching a new type with a predefined data contract. The attribute acts directly on member fields and completely bypasses any logical properties used to access these fields. It would be better to enable these properties in order to add their values ​​when accessing the fields. In conclusion, there is no direct support for the version, as the formatter presumably captures all versions of the Information. Consequently, it is difficult to handle versioning time.

+4
source

All Articles