title: WCF: Configuring Known Types
body: <p>I want to know as to how to configure known types in WCF. For example, I have a <strong>Person</strong> class and an <strong>Employee</strong> class. The <strong>Employee</strong> class is a sublass of the <strong>Person</strong> class. Both class are marked with a <code>[DataContract]</code> attribute.</p>
  
  <p>I dont want to hardcode the known type of a class, like putting a <code>[ServiceKnownType(typeof(Employee))]</code> at the Person class so that WCF will know that Employee is a subclass of Person.</p>
  
  <p>Now, I added to the host's App.config the following XML configuration:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
    &lt;system.runtime.serialization&gt;
      &lt;dataContractSerializer&gt;
        &lt;declaredTypes&gt;
          &lt;add type="Person, WCFWithNoLibrary, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"&gt;
            &lt;knownType type="Employee, WCFWithNoLibrary, Version=1.0.0.0,Culture=neutral, PublicKeyToken=null" /&gt;
          &lt;/add&gt;
        &lt;/declaredTypes&gt;
      &lt;/dataContractSerializer&gt;
    &lt;/system.runtime.serialization&gt;
    &lt;system.serviceModel&gt;
      ....... 
    &lt;/system.serviceModel&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>I compiled it, run the host, added a service reference at the client and added some code and run the client. But an error occured:</p>
  
  <blockquote>
    <p>The formatter threw an exception while
    trying to deserialize the message:
    There was an error while trying to
    deserialize parameter
    <code>http://www.herbertsabanal.net:person</code>.
    The InnerException message was 'Error
    in line 1 position 247. Element
    <code>'http://www.herbertsabanal.net:person'</code>
    contains data of the
    <code>'http://www.herbertsabanal.net/Data:Employee'</code>
    data contract. The deserializer has no
    knowledge of any type that maps to
    this contract. Add the type
    corresponding to 'Employee' to the
    list of known types - for example, by
    using the KnownTypeAttribute attribute
    or by adding it to the list of known
    types passed to
    DataContractSerializer.'.  Please see
    InnerException for more details.</p>
  </blockquote>
  
  <p>Below are the data contracts:</p>
  
  <pre><code>[DataContract(Namespace="http://www.herbertsabanal.net/Data", Name="Person")]
      class Person
      {
          string _name;
          int _age;
  
          [DataMember(Name="Name", Order=0)]
          public string Name
          {
              get { return _name; }
              set { _name = value; }
          }
  
          [DataMember(Name="Age", Order=1)]
          public int Age
          {
              get { return _age; }
              set { _age = value; }
          }
      }
  
  
  [DataContract(Namespace="http://www.herbertsabanal.net/Data", Name="Employee")]
      class Employee : Person
      {
          string _id;
  
          [DataMember]
          public string ID
          {
              get { return _id; }
              set { _id = value; }
          }
      }
  </code></pre>
  
  <p>Btw, I didn't use class libraries (WCF class libraries or non-WCF class libraries) for my service. I just plain coded it in the host project.</p>
  
  <p>I guess there must be a problem at the config file (please see config file above). Or I must be missing something. Any help would be pretty much appreciated.</p>