tags:

views:

216

answers:

3

Which one should be use in wcf NetDataContractAttribute or DataContractAttribute?

the data class as follows

namespace ABC
{
    [Serializable]
    public class DeviceConf
    {
        public DeviceConf() {}

        [XmlElement(ElementName = "ProtocolName")]
        public string ProtocolName{ get; set; }
        [XmlElement(ElementName = "Type")]
        public string TypeName { get; set; }
    }
}
+2  A: 

For the widest portability, just use [DataContract] and [DataMember]. One minor note: it won't use your contructor (this often surprises people), and it isn't guaranteed to produce identical xml (especially if you use attributes). If you want controllable xml (rather than WCF's preferred format, which offers less control) then there are ways to convince WCF to use XmlSerializer instead of DataContractSerializer - by marking the service-contract (interface) with [XmlSerializerFormat].

Marc Gravell
yes marc..but i am still confuse about the usage of DataContract vs NetDataContract..one difference is regarding the constructor is there a way to findout which one is better?
Jaswant Agarwal
Better at what? That is subjective. If it works, I would argue that DataContract is better since it is more portable, and I care about portability.
Marc Gravell
+1  A: 

It really depends on what type of interoperability you need. The example class has each property as a string therefore you're not dealing with anything specific to .NET. Either serializer (DataContract or NetDataContract) would work, however, the NetDataContractSerializer is to allow the transfer of .NET specific items over the wire that may not exist on the other side because the client is java.

If you know that you'll never expose your service to a .NET consumer then the NetDataContract would be fine for all purposes. If you know that you'll need interop the DataContract is your best bet.

Jarrod268
-1: This answer is very misleading. `DataContractSerializer` works well with types other than strings, and your answer suggests that it does not.
John Saunders
Reworded the comment to less ambiguous.
Jarrod268
+2  A: 

You should use the Data Contract Serializer for most purposes. You should remove the Serializable attribute from your class (the XML Serializer pays no attention to it in any case). Also, you should remove the various XML Attributes from your class. If you need to control the element names, then you should do that with the [DataMember] attribute.

John Saunders
it is helpfull please elaborate some more
Jaswant Agarwal
Please say what kind of information you're looking for.
John Saunders