Search code examples
wcfserializationknown-types

WCF datacontract knowntype on base class


I have a data contract in a project (Comp.DataContracts) that doesn't need to know about any other projects. I have Comp.ProjA that references Comp.DataContracts and has a derived class that just helps populate the fields of the data contract but isn't related to the datacontract. I want to tell the channel to make a service call using my derived type as the parameter but have it serialize as the base type.

Since I don't want the service or data contract projects to know about other projects how do I do this? Can I explicitly tell datacontractserializer to serialize as base instead of derived? I can always turn to a factory to populate and return the correct type but was hoping to not do that.

How do I do this without KnownType on the base class? Ideas?


Solution

  • If the base type is concrete, you can use an IDataContractSurrogate implementation to serialize all instances of Derived as Base. Otherwise I don't think it can be done without changing the base class to add [KnownType] or the service contract to add [ServiceKnownType].