views:

72

answers:

2

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?

+3  A: 

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.

Darin Dimitrov
Wow, Thanks Darin. Much appreciated. Just to confirm....You mean to say if I have .net version before 3.5 SP1, I should receive some compile time error?
Aakash
No, you should receive a runtime exception.
Darin Dimitrov
+4  A: 

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 not use the DataContract attribute, and be explicit about your data contracts. This will enableyou to tap into data contract features sucs versioning.

Steve Cooper
Thanks for explanation Steve.
Aakash
+1 I would tend to agree with Juval - it's convenient, but you're loosing something, too, if you don't explicitly set the [DataContract] and [DataMember] attributes....
marc_s