0

I have a question about the [DataContract] attribute.

I have written my code like below: here I am not using [DataContract] attribute for my test class.

 class test { [Datamember] public string Strproperty { get; set; } [Datamemer] public string Strproperty2 { get; set; } } class checktotal:Iservice { public string testmethod(test obj) { return obj.Strproperty+Strproperty2; } } 

For that I am sending data from client I am getting the values correctly.

Here is it necessary to use [DataContract] attribute for that test class?

If I removed [Datamember] for test class property is getting error while sending from client. But I am not getting any errors even if I am not using the [DataContract] attribute.

Please give me a brief explanation with example so that I can understand when to give that attribute and when do not give that attribute.

Thanks, Satya Pratap.

2 Answers 2

1

The DataContractSerializer can deal with classes that do not have the DataContract attribute if they provide a default constructor. See the MSDN documentation for more details.

Sign up to request clarification or add additional context in comments.

Comments

1

As of .NET 3.5 Service Pack 1, you can omit (not use) the [DataContract] and [DataMember] attributes. If you do that, then the DataContractSerializer in WCF will behave just like the XML serializer - it will serialize all public properties only.

I prefer to use [DataContract] and [DataMember] explicitly anyway - it gives me the opportunity to specify options (like the data contract's XML namespace, the order of the [DataMember]) and it lets me e.g. also exclude certain properties from serialization.

As soon as you start using [DataMember] on one property, then only those properties decorated with a [DataMember] will be looked at for the WCF serialization.

2 Comments

you mean can we mention that class with [DataContractSerializer] attribute? Explain it little bit briefly.I am using .net 4.0 framework.
@Balu: EITHER you use nothing at all -> then WCF's DataContractSerializer works like XmlSerializer; OR THEN you use [DataContract] and [DataMember] everywhere in your class. Pick one. You cannot mix the two approaches.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.