The Setup
I have a WCF service that exposes a base type (e.g. Animal) as well as a few derived types (e.g. Lion, Tiger, and Bear). Another type (e.g. Zoo) includes a property that is a collection of the base type. The base type is concrete, not abstract, so it is perfectly acceptable for the collection to contain instances of the base type and/or the derived types (in any combination). For example:
[DataContract, KnownType(typeof(Lion)),
KnownType(typeof(Tiger)), KnownType(typeof(Bear))]
public class Animal
{
[DataMember]
public string Species { get; set; }
}
[DataContract]
public class Lion : Animal { }
[DataContract]
public class Tiger : Animal { }
[DataContract]
public class Bear : Animal { }
[DataContract]
public class Zoo
{
[DataMember]
public List<Animal> Animals { get; set; }
}
One of my service operations accepts this type as its parameter, like so:
[ServiceContract]
public interface IZooService
{
[OperationContract]
void SetZoo(Zoo zoo);
}
All of this is well and good, and the emitted WSDL looks perfectly fine to me. It contains all of the types and correctly indicates that the derived types inherit from the base type. So, I should be able to call my service using a SOAP message such as the following:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:z="http://zoo.org">
<soapenv:Header/>
<soapenv:Body>
<z:SetZoo>
<z:Zoo>
<z:Animals>
<z:Animal>
<z:Species>Crocodile</z:Species>
</z:Animal>
<z:Tiger>
<z:Species>Bengal</z:Species>
</z:Tiger>
<z:Bear>
<z:Species>Grizzly</z:Species>
</z:Bear>
</z:Animals>
</z:Zoo>
</z:SetZoo>
</soapenv:Body>
</soapenv:Envelope>
In the above SOAP message, the Animals collection contains one instance of the base Animal type, an instance of the derived Tiger type, and an instance of the derived Bear type. WCF should be able to deserialize this message successfully.
The Problem
WCF doesn't throw any exceptions when it receives the above message. Instead, it simply ignores the derived types (Tiger and Bear) completely, and by the time deserialized message is passed to my code, the Animals collection only contains the Crocodile entry, since it is of the base type.
So, I guess I have two questions... First, why is WCF not deserializing the derived type instances in the collection. And second, since WCF obviously doesn't like something about this SOAP message, why isn't it throwing an exception? This sort of silent failure is very troubling.