{"id":2435,"date":"2014-10-16T21:30:49","date_gmt":"2014-10-16T21:30:49","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=2435"},"modified":"2014-10-16T21:30:49","modified_gmt":"2014-10-16T21:30:49","slug":"more-custom-configuration-in-the-app-config-using-iconfigurationsectionhandler","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/more-custom-configuration-in-the-app-config-using-iconfigurationsectionhandler\/","title":{"rendered":"More custom configuration in the App.config (using IConfigurationSectionHandler)"},"content":{"rendered":"<p>In the previous post I outlined how to easily create custom configuration code for accessing section of the App.config file. In some situation you may want to take things a little further, so let&#8217;s now look at using the IConfigurationSectionHandler to create the same configuration section. <\/p>\n<p>To review, let&#8217;s look at the configuration section we&#8217;re going to create<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;configSections&gt;\r\n   &lt;section name=&quot;FiltersSection&quot; type=&quot;RvMonitor.FiltersSection, RvMonitor&quot;\/&gt;\r\n&lt;\/configSections&gt;\r\n\r\n&lt;FiltersSection&gt;\r\n   &lt;Filters&gt;\r\n      &lt;add type=&quot;Filters.ClassNameFilter, Filters&quot; \/&gt;\r\n      &lt;add type=&quot;Filters.ClassNameFilter, Filters&quot; \/&gt;\r\n   &lt;\/Filters&gt;\r\n&lt;\/FiltersSection&gt; \r\n<\/pre>\n<p>So for this example, we&#8217;re not going to change this configuration and in fact we&#8217;ll keep the code which uses this configuration the same, so this will look like<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar filtersSection = (FiltersSection)ConfigurationManager.GetSection(&quot;FiltersSection&quot;);\r\n<\/pre>\n<p>The first thing we need to do is create the FiltersSection class which will implement IConfigurationSectionHandler. This requires that we implement the Create method. Now you could return the filter collection from this code instead of the FiltersSection, but I wanted to show how the two techniques would look, code-wise, for the same functionality. So in our Create method we&#8217;ll return the FiltersSection, as follows (the code below is the full implementation of the FilterSection, we&#8217;ll look at each bit of interest in a minute)<\/p>\n<p><em>Note: I will not deal with error handling within this code so I can keep it to a minimum<\/em><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class FiltersSection : IConfigurationSectionHandler\r\n{\r\n   public FiltersSection()\r\n   {\r\n      Filters = new List&lt;FilterType&gt;();\r\n   }\r\n\r\n   public object Create(object parent, object configContext, XmlNode section)\r\n   {\r\n      var serializer = new XmlSerializer(typeof(FilterType));\r\n\r\n      foreach (XmlNode node in section.ChildNodes)\r\n      {\r\n         if (node.Name == &quot;Filters&quot;)\r\n         {\r\n            foreach (XmlNode filterNode in node.ChildNodes)\r\n            {\r\n               var reader = new XmlNodeReader(filterNode);\r\n               Filters.Add((FilterType)serializer.Deserialize(reader));\r\n            }\r\n         }\r\n      }\r\n      return this;\r\n   }\r\n\r\n   public IList&lt;FilterType&gt; Filters { get; private set; }\r\n}\r\n<\/pre>\n<p>So the obvious (boring bits) are we added a Filters property to the FiltersSection and instantiated it within the constructor, but the interesting stuff is within the Create method.<\/p>\n<p>First off, as noted earlier, I wanted to mimic the behaviour of a previous post. So we return <em>this<\/em (an instance of FiltersSection) from the Create method but because of the more configurable IConfigurationSectionHandler we could have returned the filter list itself. \n\nWithin the Create method we now need to handle the XML ourselves. So if we ignore the serializer for a moment, you can see we loop through each child node within the FiltersSection - this would be any Filter elements in the App.config. When we find a Filters element we loop through the child nodes of this element, given us the \"add\" elements. Obviously you can parse\/handle this XML however you want, but here I've decided to simply deserialize it to FilterType objects and add to the Filters collection. \n\nHere's the FilterType code\n\n[code language=\"csharp\"]\n[Serializable]\n[XmlRoot(&quot;add&quot;)]\npublic class FilterType\n{\n   [XmlAttribute(&quot;type&quot;)]\n   public string TypeName { get; set; }\n}\n[\/code]\n\nAnd that's it. Simple enough. \n\nYou may feel this is simpler than the example from the previous post using the ConfigurationSection, it's certainly more configurable. I'll leave it to the reader to decide which way of implementing a custom configuration best suites them.\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous post I outlined how to easily create custom configuration code for accessing section of the App.config file. In some situation you may want to take things a little further, so let&#8217;s now look at using the IConfigurationSectionHandler to create the same configuration section. To review, let&#8217;s look at the configuration section we&#8217;re [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[86],"tags":[],"class_list":["post-2435","post","type-post","status-publish","format-standard","hentry","category-configuration"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=2435"}],"version-history":[{"count":6,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2435\/revisions"}],"predecessor-version":[{"id":2462,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2435\/revisions\/2462"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=2435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=2435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=2435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}