I'm having the following error in my wcf client.
NetDispatcherFaultException was unhandled.
The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:GetVehicleResult. The InnerException message was 'Error in line 1 position 266. Element 'http://tempuri.org/:GetVehicleResult' contains data from a type that maps to the name 'http://schemas.datacontract.org/2004/07/WCFServer:Car'. 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.
Can anyone help me where is the fault.
WCF Server
IVehicle
--------
[ServiceContract]
public interface IVehicleService
{
[OperationContract]
Vehicle GetVehicle(int type);
[OperationContract]
int GetNumberOfWheels(Vehicle vehicle);
}
VehicleService
[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;
}
}
abstract class
[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; }
}
concrete classes
[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; }
}
WCF Client
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);
}
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.