{"id":760,"date":"2013-10-25T10:39:58","date_gmt":"2013-10-25T10:39:58","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=760"},"modified":"2013-10-25T10:40:09","modified_gmt":"2013-10-25T10:40:09","slug":"net-installer-cannot-access-your-app-config","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/net-installer-cannot-access-your-app-config\/","title":{"rendered":".NET Installer cannot access your App.config"},"content":{"rendered":"<p>I&#8217;ve been caught by this once before and so it&#8217;s time to ensure it&#8217;s documented.<\/p>\n<p>I&#8217;ve created an Installer for a Windows Service in .NET, it&#8217;s a fairly generic service (or more succinctly it&#8217;s an application that allows me to create services with minimal code) so I want it to be easy to give it a service name that properly represents it&#8217;s functionality (not the generic container&#8217;s name). So I added an appSettings section to the App.config and tried to install the service using InstallUtil and ofcourse it failed to get the info. from the App.Config.<\/p>\n<p>Whilst the code for the Installer is within the EXE assembly, it&#8217;s run via InstallUtil so cannot pick up the correct App.config automatically. It&#8217;s simple to implement this functionality, see the code below<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nstring configFile = Assembly.GetExecutingAssembly().Location + &quot;.config&quot;;\r\n\r\nExeConfigurationFileMap map = new ExeConfigurationFileMap();\r\nmap.ExeConfigFilename = configFile;\r\n\r\nConfiguration config = ConfigurationManager.OpenMappedExeConfiguration(map,   \r\n                              ConfigurationUserLevel.None);\r\n\t\r\nstring serviceName = config.AppSettings.Settings&#x5B;&quot;ServiceName&quot;].Value;\r\nserviceInstaller.ServiceName = serviceName ?? DEFAULT_SERVICE_NAME;\r\n<\/pre>\n<p>In the sample above I have a fallback DEFAULT_SERVICE_NAME in case we forget to set the config as follows<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;appSettings&gt;\r\n  &lt;add key=&quot;ServiceName&quot; value=&quot;My Application&quot;\/&gt;\r\n&lt;\/appSettings&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been caught by this once before and so it&#8217;s time to ensure it&#8217;s documented. I&#8217;ve created an Installer for a Windows Service in .NET, it&#8217;s a fairly generic service (or more succinctly it&#8217;s an application that allows me to create services with minimal code) so I want it to be easy to give it [&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-760","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\/760","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=760"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/760\/revisions"}],"predecessor-version":[{"id":763,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/760\/revisions\/763"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}