WCF datacontract known for the base class

I have a contract with the data in the project (Comp.DataContracts), which is not supposed to know about any other projects. I have Comp.ProjA, which refers to Comp.DataContracts and a derived class, which simply helps to fill in the data fields of the contract, but is not associated with datacontract. I want to tell the channel to make a service call using my derived type as a parameter, but it will be serialized as the base type.

Since I do not want draft service contracts or data to know about other projects, how do I do this? Can I explicitly datacontractserializer for serialization as the base instead of the derivative? I can always turn to the factory, to complete and return the correct type, but hoped not to do so.

How can I do this without KnownType in the base class? Ideas?

+4
source share
2 answers

If the base type is specific, you can use IDataContractSurrogate implementation to serialize all instances Derived as Base. Otherwise I do not think it can be done without changing the base class to add [KnownType] or a service contract to add [ServiceKnownType].

+4
source

Can you add KnownType through the configuration file to DataContractSerializer knew of a derived type, having no rigid relationship between the projects? This would mean that the derived type will be serialized type.

Another option - use ServiceKnownType attribute, which causes a static method, and in this method to create known types using GetType(typename) . It will also allow DCS to know about the derived type if it is not part of the project or a connected project, and even if the base type is not specific.

+1
source

All Articles