Shared Transformable App/Web Config

Nice way to keep your config in one place for all types of projects is using web config transformations in addition to using shared (using Link) config files with DependentUpon back to the root config file, e.g. App.Config

Using the standard web config transforms, e.g:

In App.config

 <connectionStrings>
    <add name="Db" connectionString="Server=localhost;Port=3306;Database=myapp;
Uid=myappuser;Pwd=secret;/>
  </connectionStrings>

In App.Production.config

 <add name="Db" connectionString="Server=production;Port=3306;Database=myappprod;
UId=produser;Pwd=supersecret;" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>

This will transform your out so you have a production config file which overrides the standard config file. as documented here:
Web Config Transforms

But what if you need the config shared across projects, e.g. Unit Testing, Integration testing, Tasks project etc…

Well you can use the standard Linked file approach, where you Add an existing item but select the little drop down on the add button.
The project file items will look like below.


<ItemGroup>
	<Content Include="..\Shared\App.config">
	<Link>App.config</Link>
	<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

<Content Include="..\Shared\App.Local.config">
	<Link>App.Local.config</Link>
	<DependentUpon>App.config</DependentUpon>
</Content>

<Content Include="..\Shared\App.Staging.config">
	<Link>App.Staging.config</Link>
	<DependentUpon>App.config</DependentUpon>
</Content>

<Content Include="..\Shared\App.Production.config">
	<Link>App.Production.config</Link>
	<DependentUpon>App.config</DependentUpon>
</Content>
</ItemGroup>

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets" />

<Target Name="PostTransformAppConfig" AfterTargets="TransformWebConfig">
	<Copy Condition="Exists('$(TransformWebConfigIntermediateLocation)\transformed\App.config')" 
                  SourceFiles="$(TransformWebConfigIntermediateLocation)\transformed\App.config" DestinationFiles="$(OutputPath)\App.config" />
	<Copy Condition="Exists('$(TransformWebConfigIntermediateLocation)\transformed\App.config')" 
                  SourceFiles="$(TransformWebConfigIntermediateLocation)\transformed\App.config" DestinationFiles="$(OutputPath)\App.config" />
</Target>

Then Just Call:

  msbuild myproject.proj /t:TransformWebConfig /p:Configuration=Production

And the output will be

 <add name="Db" connectionString="Server=production;Port=3306;Database=myappprod;
UId=produser;Pwd=supersecret;"/>

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s