How to IgnoreDataMember attribute in WCF POCO Serialization

No.of Views1646
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  03 Oct 2010 08:10:32
Tag : WCF , Miscellaneous
Complex Data type for example custom class And not attributing this custom class as DataContract and properties of the class as DataMember
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

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 

Image Loading

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. 

Image Loading

Error says there is no definition for RollNumber is present at the client side.that's all thank you for reading.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top