Over the past couple of weeks I’ve been doing some WCF development and have been using WCF KnownTypes and ServiceKnownTypes to support inheritance and the use of interfaces when creating web services.
Using the KnownType and ServiceKnownTypes attribute you can have a operation contract with a signature that either returns or accepts a base class such as the one below:
This allows the web service to pass back any sub class that inherits from the IPerson interface, such as a person class or even an employee class that inherits from person.
This can be done using the KnownType attribute on the sub class associating the interface with the concrete implementation. The sub class adds the attribute [KnownType(typeof(baseclass)] passing in the generalised object or interface.
This will now allow the web service to pass back the Person sub class where the interface IPerson is used in the operation contract. However, I prefer using the ServiceKnownType attribute on the method signature, rather than using the KnownType attribute on the whole class.
ServiceKnownTypes can also be used to a number of different classes inheriting from a single base class by adding multiple attributes to the method.
The web method can now pass back either the Person or Employee object from the service.