Deprecated: Using ${var} in strings is deprecated, use {$var} instead in /home3/weo2000/public_html/offeman.com/wp-content/plugins/backup/com/core/functions.php on line 1077

Deprecated: Using ${var} in strings is deprecated, use {$var} instead in /home3/weo2000/public_html/offeman.com/wp-content/plugins/backup/com/core/functions.php on line 1077

Deprecated: Using ${var} in strings is deprecated, use {$var} instead in /home3/weo2000/public_html/offeman.com/wp-content/plugins/backup/com/core/functions.php on line 1079

Deprecated: Using ${var} in strings is deprecated, use {$var} instead in /home3/weo2000/public_html/offeman.com/wp-content/plugins/backup/com/core/functions.php on line 1079

Deprecated: Using ${var} in strings is deprecated, use {$var} instead in /home3/weo2000/public_html/offeman.com/wp-content/plugins/backup/com/core/functions.php on line 1079

Warning: Cannot modify header information - headers already sent by (output started at /home3/weo2000/public_html/offeman.com/wp-content/plugins/backup/com/core/functions.php:1077) in /home3/weo2000/public_html/offeman.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home3/weo2000/public_html/offeman.com/wp-content/plugins/backup/com/core/functions.php:1077) in /home3/weo2000/public_html/offeman.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home3/weo2000/public_html/offeman.com/wp-content/plugins/backup/com/core/functions.php:1077) in /home3/weo2000/public_html/offeman.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home3/weo2000/public_html/offeman.com/wp-content/plugins/backup/com/core/functions.php:1077) in /home3/weo2000/public_html/offeman.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home3/weo2000/public_html/offeman.com/wp-content/plugins/backup/com/core/functions.php:1077) in /home3/weo2000/public_html/offeman.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home3/weo2000/public_html/offeman.com/wp-content/plugins/backup/com/core/functions.php:1077) in /home3/weo2000/public_html/offeman.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home3/weo2000/public_html/offeman.com/wp-content/plugins/backup/com/core/functions.php:1077) in /home3/weo2000/public_html/offeman.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home3/weo2000/public_html/offeman.com/wp-content/plugins/backup/com/core/functions.php:1077) in /home3/weo2000/public_html/offeman.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831
{"id":76,"date":"2011-06-05T18:02:14","date_gmt":"2011-06-05T18:02:14","guid":{"rendered":"http:\/\/www.offeman.com\/2011\/06\/05\/OOPWithWCFDecorateDataContractWithKnownTypetypeofaType.aspx"},"modified":"2011-06-05T18:02:14","modified_gmt":"2011-06-05T18:02:14","slug":"oop-with-wcf-decorate-datacontract-with-knowntypetypeofatype","status":"publish","type":"post","link":"https:\/\/offeman.com\/oop-with-wcf-decorate-datacontract-with-knowntypetypeofatype\/","title":{"rendered":"OOP with WCF: Decorate DataContract with [KnownType(typeof(aType))]"},"content":{"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:<\/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>\n

And 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>\n

Bottom line: WCF is an amazingly powerful way to build OOP web services.<\/strong><\/p>\n

If one comments out [KnownType<\/span>(typeof<\/span>(Cat<\/span>))] and runs the program, the client will give the below runtime error:<\/p>\n

The 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}]}}