{"id":764,"date":"2013-10-29T16:22:26","date_gmt":"2013-10-29T16:22:26","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=764"},"modified":"2023-09-22T12:17:04","modified_gmt":"2023-09-22T12:17:04","slug":"separating-out-configuration-files-in-net","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/separating-out-configuration-files-in-net\/","title":{"rendered":"Separating out configuration files in .NET"},"content":{"rendered":"<p>Occasionally we might wish to de-clutter our App.config by moving parts of the configuration into separate files. Ofcourse this process is also useful where we might wish to simply reuse existing config parts.<\/p>\n<p>This capability is built into .NET, so for example we can have<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&lt;appSettings configSource=&quot;MyAppSettings.config&quot; \/&gt;\r\n<\/pre>\n<p>We can also use the appSettings attribute <em>file<\/em>, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&lt;appSettings file=&quot;MyAppSettings.config&quot; \/&gt;\r\n<\/pre>\n<p>The MyAppSettings.config file might look something like<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;\r\n&lt;appSettings&gt;\r\n  &lt;add key=&quot;configuration&quot; value=&quot;..\\..\\..\\..\\Samples\\SpaceMonitor.xml&quot;\/&gt;\r\n  &lt;add key=&quot;artifacts&quot; value=&quot;..\\..\\..\\..\\..\\Samples\\artifacts&quot;\/&gt;\r\n&lt;\/appSettings&gt;\r\n<\/pre>\n<p><em>Note: the appSettings section starts the file (after the xml declaration), i.e. we do not have a configuration section within this file.<\/em><\/p>\n<p>So this is cool, appSettings shows the configSource attribute in intellisense in Visual Studio, but we can in fact use the configSource on any section. Maybe we&#8217;ve created a section called CustomSection, then it implicitly has a configSource attribute built in.<\/p>\n<p>It&#8217;s important though to note that sectionGroups do <strong>not<\/strong> have an implicit configSource. Say we have something like<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;configSections&gt;\r\n  &lt;sectionGroup name=&quot;orchestrator&quot;&gt;\r\n    &lt;section name=&quot;aliases&quot; type=&quot;Orchestrator.Configuration.AliasesSectionHandler,   \r\n                                  Orchestrator.Configuration&quot;\/&gt;\r\n    &lt;section name=&quot;plugins&quot; type=&quot;Orchestrator.Configuration.PluginsSectionHandler, \r\n                                  Orchestrator.Configuration&quot;\/&gt;\r\n  &lt;\/sectionGroup&gt;\r\n&lt;\/configSections&gt;\r\n<\/pre>\n<p>Now you might think you can write <em>&lt;orchestrator configSource=&#8221;Orchestrator.config&#8221;\/&gt;<\/em> but I&#8217;m afraid not. <\/p>\n<p><strong>Only sections can use the configSource<\/strong><\/p>\n<p>Instead what we&#8217;d need to do is something like this<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;orchestrator&gt;\r\n  &lt;aliases configSource=&quot;Alias.config&quot; \/&gt;\r\n  &lt;plugins configSource=&quot;Plugins.config&quot;\/&gt;\r\n&lt;\/orchestrator&gt;\r\n<\/pre>\n<p>and the actual files would look something like<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;\r\n&lt;aliases&gt;\r\n  &lt;alias key=&quot;cron&quot; value=&quot;StandardTriggers.CronTrigger, StandardTriggers&quot;\/&gt;\r\n&lt;\/aliases&gt;\r\n<\/pre>\n<p>and<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;\r\n&lt;plugins&gt;\r\n  &lt;plugin type=&quot;StandardPlugins.WCFMonitorPlugin, StandardPlugins&quot;\/&gt;\r\n&lt;\/plugins&gt;\r\n<\/pre>\n<p>So with the above information you can see that if you wanted to separate out a <strong>system.serviceModel<\/strong>, for example, you would only be able to separate the behaviors, services etc. as the system.serviceModel is a sectionGroup.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Occasionally we might wish to de-clutter our App.config by moving parts of the configuration into separate files. Ofcourse this process is also useful where we might wish to simply reuse existing config parts. This capability is built into .NET, so for example we can have &lt;appSettings configSource=&quot;MyAppSettings.config&quot; \/&gt; We can also use the appSettings attribute [&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":[49],"tags":[],"class_list":["post-764","post","type-post","status-publish","format-standard","hentry","category-net"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/764","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=764"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/764\/revisions"}],"predecessor-version":[{"id":10098,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/764\/revisions\/10098"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}