开发者

Abstract classes in client lib, and concrete class in shared lib

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]
  abs开发者_StackOverflow中文版tract 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜