Opt-in Vs Opt-out in WCF

By | July 17, 2012

XmlSerializer was with Microsoft .NET since version 1.0 but with .NET framework version 3.0, Microsoft introduces DataContractSerializer. DataContractSerializer is the default serializer for WCF although we can still use XmlSerializer for serialization in Windows Communication Foundation.

A basic difference between the two Serializers is the approach being used and that is the primary goal of this WCF tutorial. DataContractSerializer uses Opt-In approach as opposite to XmlSerializer.

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.

Let’s understand it with the help of a practical example. Following code snippet clearly explains Opt-In approach.

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.

Other WCF Service Related Tutorials

Top 10 Interview Questions and Answers Series:

Category: Uncategorized Tags:

About IMRAN ABDUL GHANI

Imran Abdul Ghani is working as Software Developer(Senior) with extensive knowledge in Web development technologies especially C#, ASP.NET, MVC, WCF, Web API, ADO.NET Entity Framework, jQuery etc. He has several years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET (MCSD.NET) since 2005. You can reach his blogging at www.webdevelopmenthelp.net, www.topwcftutorials.net, and www.sharepointfordummies.net.

  • http://www.blogger.com/profile/10375369645377355897 Bryan Harnois

    I high appreciate this post. It’s hard to find the good from the bad sometimes but I think you’ve nailed it! would you mind updating your blog with more information?
    Outdoor Furniture