How to serialize a derived type as its base type with WCF

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 21:33:12

Are you using .NET 4.0? You can use the DataContractResolver for this if you are:

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractresolver.aspx

This is also basically what Entity Framework 4.0 does for its DataContractResolver for proxy types.

Here is an example: http://blogs.msdn.com/b/youssefm/archive/2009/06/05/introducing-a-new-datacontractserializer-feature-the-datacontractresolver.aspx

(see DeserializeAsBaseResolver in the link).

EDIT: If you're not using .NET 4.0, I think your next best option is a DataContractSurrogate: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.idatacontractsurrogate.aspx. ...so you can control the serialization by hand, but this can get messy.

Both are passed into the constructor of your DataContractSerializer and can be configured for WCF via the DataContractSerializerOperationBehavior: http://msdn.microsoft.com/en-us/library/system.servicemodel.description.datacontractserializeroperationbehavior.aspx.

We have just "solved" this issue by setting inherited class [DataContract(Name="BaseClass")]. It works even if inherited class is internal and defined in another project.

Hope it helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!