title: Problem with knowntype attribute in wcf
body: <p>I'm having the following error in my wcf client.</p>
  
  <blockquote>
    <p>NetDispatcherFaultException was unhandled.</p>
    
    <p>The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter <a href="http://tempuri.org/:GetVehicleResult" rel="noreferrer">http://tempuri.org/:GetVehicleResult</a>. The InnerException message was 'Error in line 1 position 266. Element '<a href="http://tempuri.org/:GetVehicleResult" rel="noreferrer">http://tempuri.org/:GetVehicleResult</a>' contains data from a type that maps to the name '<a href="http://schemas.datacontract.org/2004/07/WCFServer:Car" rel="noreferrer">http://schemas.datacontract.org/2004/07/WCFServer:Car</a>'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'Car' 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>Can anyone help me where is the fault. </p>
  
  <p>WCF Server</p>
  
  <hr>
  
  <pre><code>IVehicle
  --------
  [ServiceContract]   
  public interface IVehicleService
  {
      [OperationContract]
      Vehicle GetVehicle(int type);
  
      [OperationContract]
      int GetNumberOfWheels(Vehicle vehicle);
  }
  </code></pre>
  
  <p>VehicleService </p>
  
  <hr>
  
  <pre><code>[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]   
  public class VehicleService : IVehicleService
  {        
      public Vehicle GetVehicle(int type)
      {
          switch (type)
          {
              case 0:
                  return new Car()
                  {
                     ID = 10,
                     Brand = "Volvo",
                     SteeringWheelPosition = "left"
                  };
  
              case 1:
                  return new bike()
                  {
                      ID = 11,
                      Brand = "Scott",
                      HasFrontWheelBreak = true
                  };
  
              case 2:
                  return new Kidsbike()
                  {
                      ID = 12,
                      Brand = "Kid Scott",
                      HasFrontWheelBreak = false,
                      HasSupportingWheels = true
                  };
  
              default:
                  return null;
          }
      }
  
      public int GetNumberOfWheels(Vehicle vehicle)
      {
          return vehicle.NoOfWheels;
      }
  }
  </code></pre>
  
  <p>abstract class</p>
  
  <hr>
  
  <pre><code>[KnownType(typeof(Car))]
  [KnownType(typeof(bike))]
  [DataContract]
  public abstract class Vehicle
  {       
      [DataMember]
      public int ID { get; set; }
  
      abstract public int NoOfWheels { get; }
  
      [DataMember]
      public string Brand { get; set; }
  }
  </code></pre>
  
  <p>concrete classes</p>
  
  <hr>
  
  <pre><code>[DataContract]
  public class Car : Vehicle
  {          
      override public int NoOfWheels { get { return 4; } }
      [DataMember]
      public string SteeringWheelPosition { get; set; }  
  }
  
  [KnownType(typeof(Kidsbike))]
  [DataContract]
  public class bike : Vehicle 
  {           
      override public int NoOfWheels { get { return 2; } }
      [DataMember]
      public bool HasFrontWheelBreak { get; set; }  
  }
  
  [DataContract]
  public class Kidsbike : bike
  {
      [DataMember]
      public bool HasSupportingWheels { get; set; }  
  }
  </code></pre>
  
  <p>WCF Client</p>
  
  <hr>
  
  <pre><code>namespace WCFClient
  {
      [ServiceContract]   
      public interface IVehicleService
      {
          [OperationContract]       
          Vehicle GetVehicle(int type);
  
          [OperationContract]       
          int GetNumberOfWheels(Vehicle vehicle);
      } 
  }
  
  namespace WCFClient
  {
      [KnownType(typeof(Car))]
      [KnownType(typeof(bike))]
      [DataContract]
      public abstract class Vehicle
      {
          [DataMember]
          public int ID { get; set; }
  
          abstract public int NoOfWheels { get; }
          [DataMember]
          public string Brand { get; set; }
      }
      [DataContract]
      public class Car : Vehicle
      {
          override public int NoOfWheels { get { return 0; } }
          [DataMember]
          public string SteeringWheelPosition { get; set; }
      }
  
      [KnownType(typeof(Kidsbike))]
      [DataContract]
      public class bike : Vehicle
      {
          override public int NoOfWheels { get { return 0; } }
          [DataMember]
          public bool HasFrontWheelBreak { get; set; }
      }
      [DataContract]
      public class Kidsbike : bike
      {
          [DataMember]
          public bool HasSupportingWheels { get; set; }
      }
  }
  
  private void btnGetVehicle_Click(object sender, EventArgs e)
  {
      Car carObj = (Car)fclient.GetVehicle(0);          
  }
  </code></pre>
  
  <p>just creating proxy in client side . I can able to call the service successfully, but in response im having the problem. I try with Knowntype attribute. Whats wrong in this.</p>