WCF makes heavy use of reflection. In order to support object inheritance one needs to help WCF by decorating DataContract with [KnownType(typeof(aType))] so the service reference will include the proper information. Below is a quick example:<\/p>\n
The below data contract for an Animal also includes the known types of Cat and Dog:<\/p>\n
[DataContract<\/span>]\r\n[KnownType<\/span>(typeof<\/span>(Cat<\/span>))]\r\n[KnownType<\/span>(typeof<\/span>(Dog<\/span>))]\r\npublic class <\/span>Animal\r\n<\/span>{\r\n [DataMember<\/span>]\r\n public string <\/span>Name\r\n { get<\/span>; set<\/span>; }\r\n}\r\n\r\n[DataContract<\/span>]\r\npublic class <\/span>Dog <\/span>: Animal\r\n<\/span>{ }\r\n\r\n[DataContract<\/span>]\r\npublic class <\/span>Cat <\/span>: Animal\r\n<\/span>{ }<\/pre>\n <\/p>\n
Here, the service contract is working only with Animal:<\/p>\n
[ServiceContract<\/span>]\r\npublic interface <\/span>IServiceInterface\r\n<\/span>{\r\n [OperationContract<\/span>]\r\n string <\/span>GetAnimalName(Animal <\/span>anAnimal);\r\n\r\n}<\/pre>\nAnd the implementation is looking for specific descendant classes (Cat & Dog):<\/p>\n
public class <\/span>ServiceInstance <\/span>: IServiceInterface\r\n<\/span>{\r\n public string <\/span>GetAnimalName(Animal <\/span>anAnimal)\r\n {\r\n if <\/span>(anAnimal is <\/span>Dog<\/span>)\r\n return <\/span>"Dog"<\/span>;\r\n if <\/span>(anAnimal is <\/span>Cat<\/span>)\r\n return <\/span>"Cat"<\/span>;\r\n return <\/span>"Unknown"<\/span>;\r\n }<\/pre>\n} <\/p>\n
<\/p>\n
The calling code could look something like this:<\/p>\n
ServiceReferenceTest.ServiceInterfaceClient <\/span>aClient = new <\/span>ServiceReferenceTest.ServiceInterfaceClient<\/span>();\r\n\r\nvar <\/span>aCat = new <\/span>ServiceReferenceTest.Cat<\/span>();\r\nConsole<\/span>.WriteLine(aClient.GetAnimalName(aCat));<\/pre>\nBottom line: WCF is an amazingly powerful way to build OOP web services.<\/strong><\/p>\nIf one comments out [KnownType<\/span>(typeof<\/span>(Cat<\/span>))] and runs the program, the client will give the below runtime error:<\/p>\nThe formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter <\/em>http:\/\/tempuri.org\/:anAnimal<\/em><\/a>. The InnerException message was ‘Error in line 1 position 261. Element ‘<\/em>http:\/\/tempuri.org\/:anAnimal’<\/em><\/a> contains data from a type that maps to the name ‘<\/em>http:\/\/schemas.datacontract.org\/2004\/07\/WcfServiceWEOTest:Cat’<\/em><\/a>. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to ‘Cat’ to the list of known types – for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.’. Please see InnerException for more details.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"WCF makes heavy use of reflection. In order to support object inheritance one needs to help WCF by decorating DataContract with [KnownType(typeof(aType))] so the service reference will include the proper information. Below is a quick example: The below data contract for an Animal also includes the known types of Cat and Dog: [DataContract] [KnownType(typeof(Cat))] [KnownType(typeof(Dog))] […]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/offeman.com\/wp-json\/wp\/v2\/posts\/76"}],"collection":[{"href":"https:\/\/offeman.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/offeman.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/offeman.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/offeman.com\/wp-json\/wp\/v2\/comments?post=76"}],"version-history":[{"count":0,"href":"https:\/\/offeman.com\/wp-json\/wp\/v2\/posts\/76\/revisions"}],"wp:attachment":[{"href":"https:\/\/offeman.com\/wp-json\/wp\/v2\/media?parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/offeman.com\/wp-json\/wp\/v2\/categories?post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/offeman.com\/wp-json\/wp\/v2\/tags?post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}