<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Trevor Power &#187; app.config</title>
	<atom:link href="http://blog.trevorpower.com/index.php/tag/app-config/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.trevorpower.com</link>
	<description>Software development and other thoughts</description>
	<lastBuildDate>Fri, 20 Aug 2010 07:33:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Sharing configuration settings in Visual Studio</title>
		<link>http://blog.trevorpower.com/index.php/sharing-configuration-settings-in-visual-studio/</link>
		<comments>http://blog.trevorpower.com/index.php/sharing-configuration-settings-in-visual-studio/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 20:30:15 +0000</pubDate>
		<dc:creator>trevorpower</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[app.config]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[web.config]]></category>

		<guid isPermaLink="false">http://blog.trevorpower.com/?p=118</guid>
		<description><![CDATA[Correctly managing your systems settings during both development and deployment can save you a lot of time. When ever you have to manually change settings there is the opportunity to mess up, so I try to automate any repetitive configuration tasks.
It often happens that application settings or connection strings are needed by more than one [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.trevorpower.com%2Findex.php%2Fsharing-configuration-settings-in-visual-studio%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.trevorpower.com%2Findex.php%2Fsharing-configuration-settings-in-visual-studio%2F" height="61" width="51" /></a></div><p>Correctly managing your systems settings during both development and deployment can save you a lot of time. When ever you have to manually change settings there is the opportunity to mess up, so I try to <a href="http://blog.trevorpower.com/index.php/2009/02/handling-multiple-web-config-files-in-visual-studio/">automate any repetitive configuration tasks</a>.</p>
<p>It often happens that application settings or connection strings are needed by more than one component in your system. The solution I use is a variation of this method of <a href="http://devlicio.us/blogs/derik_whittaker/archive/2008/04/15/how-to-share-configuration-files-between-projects.aspx">sharing app.config files between applications</a>, except that it allows each project to have its own app.config or web.config files which reference the common settings.</p>
<h2>Creating shared settings</h2>
<p>The first step is to make a settings class that acts as a wrapper around the values in the configuration files. This is a very simple class inheriting ApplicationSettingsBase and looks like this:</p>
<pre name="code" class="c#:nocontrols:nogutter">using System.Configuration;

namespace TP.Example.Configuration
{
    public class Settings : ApplicationSettingsBase
    {
        private static Settings _defaultInstance =
                (Settings)(ApplicationSettingsBase.Synchronized(new Settings()));

        public static Settings Default { get { return _defaultInstance; } }

        [ApplicationScopedSettingAttribute()]
        string ServiceName
        {
            get { return ((string)(this["ServiceName"])); }
            set { this["ServiceName"] = value; }
        }

        [ApplicationScopedSettingAttribute()]
        string EventLogName
        {
            get { return ((string)(this["EventLogName"])); }
            set { this["EventLogName"] = value; }
        }

        [ApplicationScopedSettingAttribute()]
        [SpecialSettingAttribute(SpecialSetting.ConnectionString)]
        string ConnectionString
        {
            get { return ((string)(this["ConnectionString"])); }
            set { this["ConnectionString"] = value; }
        }
    }
}</pre>
<p>The next step is to create the files that contain the settings. I usually have two, one for application settings and one for connection strings. The application settings file consists of a single node matching the full name of my settings class:</p>
<pre name="code" class="xml:nocontrols:nogutter">&lt;TP.Example.Configuration.Settings&gt;
  &lt;setting name="ServiceName" serializeAs="String"&gt;
    &lt;value&gt;My Example Service
  &lt;/setting&gt;
  &lt;setting name="EventLogName" serializeAs="String"&gt;
    &lt;value&gt;ExampleLogName
  &lt;/setting&gt;
&lt;/TP.Example.Configuration.Settings&gt;</pre>
<p>The connection strings file consists of one &#8216;connectionStrings&#8217; node. Note the name of the connection string is qualified with the full name of my settings class.</p>
<pre name="code" class="xml:nocontrols:nogutter">&lt;connectionStrings&gt;
  &lt;add name="TP.Example.Configuration.Settings.ConnectionString" connectionString="[...]" /&gt;
&lt;/connectionStrings&gt;</pre>
<p>These files should exist outside any of your projects but be under source control. I usually put them in a solution folder &#8216;Configuration&#8217; that contains a simple project with the above settings class:<br />
<img class="alignleft size-full wp-image-128" title="'Configuration' solution folder" src="http://blog.trevorpower.com/wp-content/uploads/2009/10/ConfigFolder.png" alt="'Configuration' solution folder" width="351" height="145" /><br clear="all"/></p>
<h2>Referencing shared settings</h2>
<p><img class="alignright size-full wp-image-150" title="Icons have shortcut arrow" src="http://blog.trevorpower.com/wp-content/uploads/2009/10/FilesAsLinks.png" alt="FilesAsLinks" width="326" height="129" />These common settings are now ready to be used by any project in the solution by following these steps:</p>
<ol>
<li>Add a reference to the settings project.</li>
<li>Add a link to settings files and turn on &#8216;copy to output directory&#8217;</li>
<li>Include settings files in the app.config/web.config</li>
</ol>
<p>To link to the settings files you should <em>Add </em>-&gt; <em>Existing Item</em> -&gt; <em>Add As Link</em>, they will then appear with a shortcut icon. This means that they only point to the original file, so there is no duplication.</p>
<p>Now that the files are in our project we just need to get our App.config file to include them. A simplified App.config would look like this:</p>
<pre name="code" class="xml:nocontrols:nogutter">&lt;?xml version="1.0" encoding="utf-8" ?>
&lt;configuration>
  &lt;configSections>
    &lt;sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup" >
      &lt;section name="TP.Example.Configuration.Settings" type="System.Configuration.ClientSettingsSection" />
    &lt;/sectionGroup>
  &lt;/configSections>
  &lt;connectionStrings configSource="TP.Example.Configuration.ConnectionStrings.config" />
  &lt;applicationSettings>
    &lt;TP.Example.Configuration.Settings configSource="TP.Example.Configuration.AppSettings.config" />
  &lt;/applicationSettings>
&lt;/configuration>
</pre>
<p>Now when you modify one of these files the change will be picked up by all other projects in the solution.
<p/>
<h2>Websites and Web.config</h2>
<p>Using these settings in a website requires one extra step. As before, the settings file will be linked to by the project file and the &#8216;Copy to Output Directory&#8217; turned on. The problem is that the web.config file is usually at the root while the output directory is &#8216;bin&#8217; or &#8216;bin/Debug&#8217; depending on your setup. Because the output folder can depend on the build configuration, I have found the best solution is to copy the config files from the target directory to the project directory. This is just one line in the build events.<img src="http://blog.trevorpower.com/wp-content/uploads/2009/10/CopyConfigFiles.png" alt="COPY $(TargetDir)*.config $(ProjectDir)" title="COPY $(TargetDir)*.config $(ProjectDir)" width="532" height="267" class="aligncenter size-full wp-image-169" /><br />
Writing it out like this sounds like a lot of work but it is easy to setup and easy to maintain. If you have any question or have a better solution, please leave a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.trevorpower.com/index.php/sharing-configuration-settings-in-visual-studio/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

