.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> WCF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

IgnoreDataMember attribute in WCF POCO Serialization

Posted By:Dhananjay Kumar       Posted Date: October 03, 2010    Points: 50    Category: WCF    URL: http://www.dotnetspark.com  

In this article we will see about IgnoreDataMember attribute in WCF POCO Serialization.
 

POCO stands for PLAIN OLD CLR OBJECT


If in WCF we are using 
  1. Complex Data type  for example custom class 
  2. And not attributing this custom class as DataContract  and properties of the class as DataMember 
  3. If we want to expose the below custom class  to client  and not using DataContractSeraliazer  then all public properties will be automatically serialized
public class MyCustomData
{
   public string Name { get; set; }
   public string RollNumber { get; set; }
   public string Subject { get; set; }
}

Automatically POCO (Plain old CLR Object) is serialized without using DataContract attribute.  


So to test this, let us create service contract 

[ServiceContract]
public interface IService1
{
   [OperationContract]
   MyCustomData GetData();
}

And service implementation is as below 

public class Service1 : IService1
{
   public MyCustomData GetData()
   {
       MyCustomData data = new MyCustomData { Name = "John", RollNumber = "1", Subject = "WCF" };
       return data;
   }
}

So when we consume this service at client side  


1.gif

We can use that as above 


Program.cs 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication1.ServiceReference1;


namespace ConsoleApplication1
{
   class Program
   {
       static void Main(string[] args)
       {
           Service1Client proxy = new Service1Client();
           MyCustomData data = proxy.GetData();
           Console.WriteLine(data.Name + data.Subject + data.RollNumber);
           Console.ReadKey(true);
       }
   }
}



Now we know how we could serialize custom data or class without using DataContractSerlializer.  But if we notice all the public property are serialized. If we want to hide some of public property from serialization then we will use IgnoreDataMember


public class MyCustomData
{
   public string Name { get; set; }
   [IgnoreDataMember]
   public string RollNumber { get; set; }
   public string Subject { get; set; }
}

Add the namespace System.RunTime.Serilization to work with IgnoreDataMember attribute. 


Now we are hiding public property RollNumber with IgnoreDataMember attribute , so at the client side when we try to access RollNumber property on custom class MyCustomData we will get the below compile time error. 


2.gif

Error says there is no definition for RollNumber is present at the client side. 


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend