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 ba开发者_如何学运维se 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?
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].
Can't you add the KnownType
through the config file, so that the DataContractSerializer will know about the derived type without there being the hard dependency between the projects? this will mean that the derived type will be the serialized type though.
The other option is to use a ServiceKnownType attribute which calls a static method and in that method create the known types using GetType(typename)
. This will also allow the DCS to know about the derived type if it not part of the project or referenced project and even if the base type is not concrete.
精彩评论