Managing connection strings, app settings, log4net settings, SMTP settings, etc between developers and environments can be a major pain in the ass. I’ve used numerous approaches for this in the past but none of them completely satisfied me. That has, finally, come to an end.
The approach I have laid out here will allow developers to have their own personal settings and not have to worry about modifying any .config files each time they perform an update from source control. This approach will also allow you to easily manage your environment specific settings via your Build server.
Step 1 – Environmental Variable
Each developer must create an environmental variable that points to the root for their local working directory.
Step 2 – Project Config Directory
Create a Config directory under the development directory’s root. Within this Config directory each developer should have a subdirectory for his/her machine name. So, for example:
- Local working directory for project: D:\Projects\SomeProject\
- Environment variable named MyRoot maps to D:\Projects\SomeProject\
- Project config path: D:\Projects\SomeProject\Config\
- Personal config path: D:\Projects\SomeProject\Config\BEAU-PC\
- The above path for my personal configuration should then be resolvable via: %MyRoot%\Config\BEAU-PC\
Step 3 – Project .targets File
This .targets file can contain the properties, targets, etc that are common to all projects. You will need, at least, the below target which will copy the .config files found in your personal config path to a directory named “Config” in the output assembly’s bin directory:
<Target Name="AfterBuild" Condition="Exists('$(MyRoot)\Config\$(ComputerName)\')">
<ItemGroup>
<ConfigFiles Include="$(MyRoot)\Config\$(ComputerName)\*.config" />
</ItemGroup>
<Copy SourceFiles="@(ConfigFiles)" DestinationFolder="$(TargetDir)\Config\" />
</Target>
Note: the above target must be placed inside the normal MSBuild project structure.
Step 4 – Modify Projects
You must now modify all .csproj (or .vbproj) files so that they import the above .targets file. This is as simple as opening the project file in a text editor and finding the Microsoft.CSharp.targets import and placing your import directly after it (if it’s not after it then it will not fire, as the “AfterBuild” target logic is defined in the Microsoft.CSharp.targets file).
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(MyRoot)\SomeProject.targets" />
Step 5 – Application Configuration
Within your app.config (or web.config) any machine/environment specific settings can now be stored in separate .config files found in the machine specific directory mentioned above. You can then use the configSource attribute to reference these settings. For example:
<appSettings configSource="Config\AppSettings.config" />
<connectionStrings configSource="Config\ConnectionStrings.config" />
You should note that, for web applications, the configSource paths are evaluated relative to the web root and not the bin directory. This means your configSource paths should look like:
bin\Config\ConnectionStrings.config
Hopefully this makes your life easier.