WCF DataContract Exclude data from being serialized in derived classes

I have a base class with DataMember properties. I also have a derived class with DataMember properties. In my WCF project, I am returning a derived class. Is there a way to prevent a member in my base class from being serialized? Here is a sample code:

public class BaseClass { public string ShortDescription {get;set;} public string LongDescription {get;set;} } public class DerivedClass : BaseClass { public List<Description> Descriptions {get;set;} } 

In this code, I want to be able to hide the inherited members of ShortDescription and LongDescription, because they are now deprecated. Any attempts to do this were unsuccessful. Here is what I tried:

 public class DerivedClass : BaseClass { [DataMember] public List<Description> Descriptions {get;set;} // override the base class members [IgnoreDataMember] public override string ShortDescription {get;set;} [IgnoreDataMember] public override string LongDescription {get;set;} } 

and

 public class DerivedClass : BaseClass { [DataMember] public List<Description> Descriptions {get;set;} // shadow the base class members [IgnoreDataMember] public new string ShortDescription {get;set;} [IgnoreDataMember] public new string LongDescription {get;set;} } 

None of these approaches worked. The DerivedClass type, when output to WSDL, still contains the "ignored" elements of the base class.

You may wonder why I am not just changing the base class. This is because I still use the base class in its original form as a previous version of the WSDL type to support backward compatibility for consumers. So I can have a call to v1000 that returns BaseClass and a call to V1010 that returns DerivedClass. I can add and change functionality to DerivedClass, all I want is without the ability to affect consumers of v1000 functionality.

+7
wcf
source share
3 answers

In the end, I found a suitable way to handle this task. I have no code in front of me, so I'm going to use the syntax. This is a fairly simple solution, but it solves the specific problem I am facing.

 public class BaseClass { // leave this guy empty } public class DerivedClassVersion1 : BaseClass { [DataMember] public string ShortDescription {get;set;} [DataMember] public string LongDescription {get;set;} } public class DerivedClassVersion2 : BaseClass { [DataMember] public List<Description> Descriptions {get;set;} } 

Badda Bing! Pretty simple, but that's what I need.

+2
source share

Try adding the [DataContract] attribute to both classes. This will allow the serializer to view the [DataMember] and [IgnoreDataMember] attributes. Without class assignment using [DataContract], all public members are serialized.

 [DataContract] public class BaseClass { [IgnoreDataMember] public string ShortDescription {get;set;} [IgnoreDataMember] public string LongDescription {get;set;} } [DataContract] public class DerivedClass : BaseClass { [DataMember] public List<Description> Descriptions {get;set;} } 

In addition, it would be even better to add a namespace to your data by specifying it in the [DataContract (Namespace = "...")] attribute. This would break the old customers calling your updated service.

+6
source share

Try the following:

 public class BaseClass { [DataMember] public virtual string ShortDescription {get;set;} [DataMember] public virtual string LongDescription {get;set;} } public class DerivedClass : BaseClass { [DataMember] public List<Description> Descriptions {get;set;} public override string ShortDescription {get;set;} public override string LongDescription {get;set;} } 

EDIT: Perhaps using messaging contracts to control serialization exactly can work: http://msdn.microsoft.com/en-us/library/ms730255.aspx

Otherwise, you can also explore the implementation of "WCF Router", which allows you to use the same endpoint, but direct different versions to different services.

0
source share

All Articles