{"id":183,"date":"2015-05-23T02:07:07","date_gmt":"2015-05-23T02:07:07","guid":{"rendered":"http:\/\/www.wellcode.de\/home\/?p=183"},"modified":"2018-06-15T01:36:46","modified_gmt":"2018-06-15T01:36:46","slug":"add-known-types-to-wcf-service-dynamically-knowntypeprovider","status":"publish","type":"post","link":"https:\/\/wellcode.de\/home\/index.php\/en\/add-known-types-to-wcf-service-dynamically-knowntypeprovider\/","title":{"rendered":"add known types to wcf service dynamically &#8211; KnownTypeProvider"},"content":{"rendered":"<p>After some experiences I camed out to the following code. It takes care of finding the missing types used in an operation contract.<\/p>\n<p>If you want to use this code, the only thing to do here, is to extend the assemblies list of all your assemblies that contain data contract classes, whitch are part of the operation contracts in use.<\/p>\n<pre class=\"lang:default decode:true \">using System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Reflection;\r\nusing System.Runtime.Serialization;\r\nusing System.Web;\r\n\r\nnamespace Wellcode.Wello\r\n{\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ data contract type collector\r\n    \/\/\/ &lt;\/summary&gt;\r\n    public static class KnownTypeProvider\r\n    {\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ Collect types of all data contracts in all assemblies of \r\n        \/\/\/ interest. this are\r\n        \/\/\/ - WellFrame Assembly, where basic types are definied\r\n        \/\/\/ - Assembly, that contains generated entity types, used in the web service.\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=\"provider\"&gt;&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n        public static IEnumerable&lt;Type&gt; GetKnownTypes(ICustomAttributeProvider provider)\r\n        {\r\n            if (types != null)\r\n                return types;\r\n\r\n            types = new List&lt;Type&gt;();\r\n\r\n            var assemblies = new List&lt;Assembly&gt;();\r\n            assemblies.Add(typeof(Wellcode.Frame.WebEntity).Assembly);\r\n            assemblies.Add(typeof(Wellcode.Wello.Document).Assembly);\r\n\r\n            foreach (var assembly in assemblies)\r\n            {\r\n                foreach (var type in assembly.ExportedTypes)\r\n                {\r\n                    var attr = type.GetCustomAttribute&lt;DataContractAttribute&gt;(false);\r\n                    if (attr != null &amp;&amp; !type.IsGenericType)\r\n                    {\r\n                        types.Add(type);\r\n                    }\r\n                }\r\n            }\r\n\r\n            return types;\r\n        }\r\n\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ static list of found types\r\n        \/\/\/ &lt;\/summary&gt;\r\n        private static List&lt;Type&gt; types = null;\r\n    }\r\n}<\/pre>\n<p>the following example shows the using of the provider.<\/p>\n<pre class=\"lang:default decode:true \">namespace Wellcode.Wello\r\n{\r\n    public partial interface IWelloService\r\n    {\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ get an entity 'AccingAccount' using its primary keys\r\n        \/\/\/ Table AccingAccount\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;returns&gt;Entity 'AccingAccount'&lt;\/returns&gt;\r\n        [OperationContract]\r\n        [ServiceKnownType(\"GetKnownTypes\", typeof(KnownTypeProvider))]\r\n        AccingAccount GetAccingAccount(int accountId);\r\n    }\r\n}<\/pre>\n<p>Related to <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/framework\/wcf\/\" target=\"_blank\" rel=\"noopener\"><em>Microsoft Windows Communication Foundation (WCF)<\/em><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>After some experiences I camed out to the following code. It takes care of finding the missing types used in an operation contract. If you want to use this code, the only thing to do here, is to extend the assemblies list of all your assemblies that contain data contract classes, whitch are part of &hellip; <a href=\"https:\/\/wellcode.de\/home\/index.php\/en\/add-known-types-to-wcf-service-dynamically-knowntypeprovider\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">add known types to wcf service dynamically &#8211; KnownTypeProvider<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-183","post","type-post","status-publish","format-standard","hentry","category-blog"],"_links":{"self":[{"href":"https:\/\/wellcode.de\/home\/index.php\/wp-json\/wp\/v2\/posts\/183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wellcode.de\/home\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wellcode.de\/home\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wellcode.de\/home\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wellcode.de\/home\/index.php\/wp-json\/wp\/v2\/comments?post=183"}],"version-history":[{"count":8,"href":"https:\/\/wellcode.de\/home\/index.php\/wp-json\/wp\/v2\/posts\/183\/revisions"}],"predecessor-version":[{"id":1280,"href":"https:\/\/wellcode.de\/home\/index.php\/wp-json\/wp\/v2\/posts\/183\/revisions\/1280"}],"wp:attachment":[{"href":"https:\/\/wellcode.de\/home\/index.php\/wp-json\/wp\/v2\/media?parent=183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wellcode.de\/home\/index.php\/wp-json\/wp\/v2\/categories?post=183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wellcode.de\/home\/index.php\/wp-json\/wp\/v2\/tags?post=183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}