Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to accept a collection of a base type in WCF
    primarykey
    data
    text
    <p><strong>The Setup</strong></p> <p>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, <em>not</em> 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:</p> <pre><code>[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&lt;Animal&gt; Animals { get; set; } } </code></pre> <p>One of my service operations accepts this type as its parameter, like so:</p> <pre><code>[ServiceContract] public interface IZooService { [OperationContract] void SetZoo(Zoo zoo); } </code></pre> <p>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:</p> <pre><code>&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:z="http://zoo.org"&gt; &lt;soapenv:Header/&gt; &lt;soapenv:Body&gt; &lt;z:SetZoo&gt; &lt;z:Zoo&gt; &lt;z:Animals&gt; &lt;z:Animal&gt; &lt;z:Species&gt;Crocodile&lt;/z:Species&gt; &lt;/z:Animal&gt; &lt;z:Tiger&gt; &lt;z:Species&gt;Bengal&lt;/z:Species&gt; &lt;/z:Tiger&gt; &lt;z:Bear&gt; &lt;z:Species&gt;Grizzly&lt;/z:Species&gt; &lt;/z:Bear&gt; &lt;/z:Animals&gt; &lt;/z:Zoo&gt; &lt;/z:SetZoo&gt; &lt;/soapenv:Body&gt; &lt;/soapenv:Envelope&gt; </code></pre> <p>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.</p> <p><strong>The Problem</strong></p> <p>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.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload