I am learning wcf, and I am seeing this opt in and opt out serialization. I am still scratching my head. I have already seen this SO post. But it didn't help. Can someone explain me succinctly what it is?
Asked
Active
Viewed 211 times
1 Answers
3
actually its so simple: Opt-In approach says properties that are considered to be part of DataContract must be explicitly marked othewise will be ignore, while Opt-Out means all of the properties will be assumed to be part of the DataContract unless marked explicitly.
namespace MySchoolService
{
[DataContract]
public class Student
{
[DataMember]
public string StudentNumber;
[DataMember]
public string FirstName;
[DataMember]
public string LastName;
public string MarksObtained;
}
[ServiceContract]
public interface IStudentService
{
//Service Code Here.
}
}
In above code StudentNumber
, FirstName
, LastName
properties of Student
class are explicitly marked with DataMember
attribute as oppose to MarksObtained
, so MarksObtained
will be ignored.
Below code represents an example of Opt-Out approach.
namespace MySchoolService
{
[Serializable()]
public class Student
{
public string StudentNumber;
public string FirstName;
public string LastName;
[NonSerialized()]
public string marksObtained;
}
[ServiceContract]
public interface IStudentService
{
//Service Code Here.
}
}
In above example, we explicitly marked MarksObtained
property as [NonSerialized()]
attribute, so it will be ignored except the others.
hope could help you.

Mohammad
- 2,724
- 6
- 29
- 55