views:

397

answers:

1

I've given a fair read through msdn:datacontracts and I cannot find a out why the following does not work. So what is wrong here? Why isn't ExtendedCanadianAddress recognized by the datacontract serializer?

Type 'XYZ.ExtendedCanadianAddress' with data contract name 'CanadianAddress:http://tempuri.org/Common/Types' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

Given:

namespace ABC  
{  
 [KnownType(typeof(Address))] 
 public abstract class Z  
 {
   //stuff
   //method that adds all types() in namespace to self
 }

 [KnownType(typeof(CanadianAddress))]  
 [DataContract(Name = "Address", Namespace = "http://tempuri.org/Types")]  
 public class Address : Z
 {}

 [DataContract(Name = "CanadianAddress", Namespace = "http://tempuri.org/Types")]
 public class CanadianAddress : Address
 {}
}

namespace XYZ
{
 [KnownType(typeof(ExtendedCanadianAddress))
 [DataContract(Name = "Address", Namespace = "http://tempuri.org/Types")] 
 public class ExtendedAddress : Address
 {
   //this serializes just fine
 }
 [DataContract(Name = "CanadianAddress", Namespace = "http://tempuri.org/Types")] 
 public class ExtendedCanadianAddress : CanadianAddress
 {
   //will NOT serialize
 }
}
+1  A: 

I will try an repro, but right off the bat the attributes look wrong...

[DataContact] vs [DataContract] Note the missing "r"

Also, although your c# namespaces are different your DataContract namespaces are the same and you have more then one contract with the same name

Name = "CanadianAddress", Namespace = "http://tempuri.org/Types"

JoeGeeky
What I have is a finalized contract in namespace ABC, and rewrapped contract within XYZ to wrap ABC's contract with alternate constructors.I'm trying to achieve less code by using inheritance.
Inheritance is fine, but each known implementation must have a unique `Name` or reside in a unique `Namespace`. Otherwise there is no way to match a request to the correct concrete implementation.
JoeGeeky