1
votes

How do I solve the serialization problem with abstract class defined in a shared client library, and concrete implementation in a server side library.

Interface in shared client library :

  [ServiceContract(SessionMode=SessionMode.Required)]
  [ServiceKnownType(typeof(SharedClient.Shape))]
  public interface IMyInterface
  {
    void UploadDrawing(Drawing dr);
  }

Concreate Drawing class in shared client library :

  [DataContract]
  [KnownType(typeof(SharedClient.Shape))]
  public class Drawing
  {
    public Shape s;
  }

Abstract class in shared client library :

  [DataContract]
  abstract public class Shape
  {
    [DataMember]
    public abstract string Name;
  }

Concrete class implementation in separate library which references the client library :

  [DataContract]
  public class Cirle : ClientLibrary.Shape
  {
    public override string Name { get; set; } 
  }

I keep getting the exception message:

There was an error while trying to serialize parameter http://tempuri.org/:Drawing. The InnerException message was 'Type 'Circle' with data contract name 'Circle:http://schemas.datacontract.org/2004/07/' is not expected. Consider using a DataContractResolver or 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.'. Please see InnerException for more details.

1
Please don't put things like "WCF - " in your titles. On Stack Overflow, tags serve the same purpose better.John Saunders

1 Answers

1
votes

KnownType works in other way. If you want to use KnownType attribute you must use it on the base class to define its child:

[DataContract]
[KnownType(typeof(Circle))]
abstract public class Shape
{
    [DataMember]
    public abstract string Name;
}  

That will not be too much helpful in your case. Try to put ServiceKnownType with concrete class on your interface:

[ServiceContract(SessionMode=SessionMode.Required)]
[ServiceKnownType(typeof(Circle))]
public interface IMyInterface
{
    void UploadDrawing(Drawing dr);
}

You doesn't have to define Shape as ServiceKnownType - it is already known because it is used in Drawing but WCF yet doesn't know the Circle type.