WCF Service object serialization

c#, inheritance, silverlight, wcf

Solution

WCF does not directly works on abstract classes. You shall use KnownType attributes on the datacontract or service class. below are examples;

[DataContract]
[KnownType(typeof(Client))]
public class Contact
{
   ...
}

[ServiceContract]
[ServiceKnownType(typeof(Client))]
public interface IMyService
{
    contact getcontact(Guid id);
}

Problem

I have one abstract class named contact and another class called client that inherits from contact. I'm dealing with a WCF Service with a method that takes a parameter of type contact. however what I have is an instance of client that I want to pass. Im facing this Error: Type 'xxx.Client' with data contract name 'Client:http://schemas.datacontract.org/2004/07/xxx' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

Original source