In service if we add the DataContract attribute over the class and DataMember over the properties, class will be shared across client and a server. But in case, if we do not add the DataContract and DataMember still it behaves in the same way i.e, In reference.cs
file its adding the DataContract and DataMember attributes.
So class with DataContract and without DataContract works in the same way so do we really need to add the DataContract for classes to expose it to client ?
Class in Service -
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
In Reference.cs -
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="CompositeType", Namespace="http://schemas.datacontract.org/2004/07/WcfService1")]
[System.SerializableAttribute()]
public partial class CompositeType : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {
[System.NonSerializedAttribute()]
private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
[System.Runtime.Serialization.OptionalFieldAttribute()]
private bool BoolValueField;
[System.Runtime.Serialization.OptionalFieldAttribute()]
private string StringValueField;
[global::System.ComponentModel.BrowsableAttribute(false)]
public System.Runtime.Serialization.ExtensionDataObject ExtensionData {
get {
return this.extensionDataField;
}
set {
this.extensionDataField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public bool BoolValue {
get {
return this.BoolValueField;
}
set {
if ((this.BoolValueField.Equals(value) != true)) {
this.BoolValueField = value;
this.RaisePropertyChanged("BoolValue");
}
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public string StringValue {
get {
return this.StringValueField;
}
set {
if ((object.ReferenceEquals(this.StringValueField, value) != true)) {
this.StringValueField = value;
this.RaisePropertyChanged("StringValue");
}
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName) {
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if ((propertyChanged != null)) {
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
So please let me know if i am missing something here.
"Since a lot of programmers were overwhelmed with the [DataContract] and [DataMember] attributes, with .NET 3.5 SP1, Microsoft made the data contract serializer handle all classes - even without any of those attributes
- much like the old XML serializer.
So as of .NET 3.5 SP1, you don't have to add data contract or data member attributes anymore - if you don't then the data contract serializer will serialize all public properties on your class, just like the XML serializer would."
Member
370 Points
305 Posts
Sharing the class between client and server
Nov 11, 2016 03:41 PM|abhijithmanipal|LINK
Hi All,
I have a question on the DataContracts in WCF -
In service if we add the DataContract attribute over the class and DataMember over the properties, class will be shared across client and a server. But in case, if we do not add the DataContract and DataMember still it behaves in the same way i.e, In reference.cs file its adding the DataContract and DataMember attributes.
So class with DataContract and without DataContract works in the same way so do we really need to add the DataContract for classes to expose it to client ?
So please let me know if i am missing something here.
Thanks in advance !!!
Participant
1310 Points
442 Posts
Re: Sharing the class between client and server
Nov 11, 2016 04:22 PM|deepalgorithm|LINK
There are benefits from using those attributes, though as you noticed they are not required.
Read the answer on SO by mark_s. He breaks this down in detail.
https://stackoverflow.com/questions/4836683/when-to-use-datacontract-and-datamember-attributes
"Since a lot of programmers were overwhelmed with the [DataContract] and [DataMember] attributes, with .NET 3.5 SP1, Microsoft made the data contract serializer handle all classes - even without any of those attributes - much like the old XML serializer.
So as of .NET 3.5 SP1, you don't have to add data contract or data member attributes anymore - if you don't then the data contract serializer will serialize all public properties on your class, just like the XML serializer would."