views:

209

answers:

2

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"&gt;
   <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.

A: 

There are quite a few ways of doing this in WCF. See the following blog on known types for a good description of the options.

In your example you can do it as follows:

[DataContract]
[KnownType(typeof(Tiger))]
[KnownType(typeof(Bear))]
public class Animal    
{
    [DataMember]
    public string Species { get; set; }
}
Tuzo
Sorry, I should have mentioned that I already had the KnownType attributes in place. I've updated the example code to include that.
James Messinger
A: 

Okay... I figured out the problem. It turns out that the SOAP syntax for this scenario requires a little extra work to get right. Because the Animals collection is defined as an array of Animal types, all of the child elements in the SOAP message need to be elements, even if they are actually instances of a derived type. The actual instance type is defined by the "type" attribute, which is part of the XMLSchema-instance namespace. So, my SOAP message should have looked like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:z="http://zoo.org"&gt;
<soapenv:Header/>
<soapenv:Body>
  <z:SetZoo>
     <z:Zoo>
        <z:Animals>
           <z:Animal>
              <z:Species>Crocodile</z:Species>
           </z:Animal>
           <z:Animal i:type="z:Tiger">
              <z:Species>Bengal</z:Species>
           </z:Animal>
           <z:Animal i:type="z:Bear">
              <z:Species>Grizzly</z:Species>
           </z:Animal>
        </z:Animals>
     </z:Zoo>
  </z:SetZoo>
</soapenv:Body>
</soapenv:Envelope>

Of course, that still doesn't address my other concern, which is that the WCF deserializer should throw an exception if it doesn't understand the SOAP message. It shouldn't just silently ignore parts of the message!

James Messinger