Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use <a href="http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5" rel="noreferrer">SlowCheetah</a> plugin. For more options and details of how to use SlowCheetah keep reading.</p> <p>As you have already noticed, there is no default and easy way to use different config files for a <em>Library type (.dll)</em> project. The reason is that the current thinking is: "You don't need to"! Framework developers reckon you need configuration for the executable file: be it a console, desktop, web, mobile app or something else. If you start providing configuration for a <em>dll</em>, you may end up with something I can call a <em>config hell</em>. You may no longer understand (easily) why this and that variables have such weird values coming seemingly from nowhere.</p> <p>"Hold on", - you may say, "but I need this for my integration/unit testing, and <em>it is</em> a library!". And that is true and this is what you can do (pick only one, don't mix):</p> <h1>1. SlowCheetah - transforms current config file</h1> <p>You can install <a href="http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5" rel="noreferrer">SlowCheetah</a> - a Visual Studio plug-in that does all low level XML poking (or transformation) for you. The way it works, briefly:</p> <ul> <li>Install SlowCheetah and restart Visual Studio (Visual Studio > Tools > Extensions and Updates ... > Online > Visual Studio Gallery > search for "Slow Cheetah" )</li> <li>Define your solution configurations (<em>Debug</em> and <em>Release</em> are there by default), you can add more (right click on the solution in <em>Solution Explorer</em> > <em>Configuration Manager...</em> > <em>Active Solution Configuration</em> > <em>New...</em></li> <li>Add a config file if needed</li> <li>Right click on config file > <em>Add Transform</em> <ul> <li>This will create Transformation files - one per your configuration</li> <li>Transform files work as injectors/mutators, they find needed XML code in the original config file and inject new lines or mutate needed value, whatever you tell it to do</li> </ul></li> </ul> <h1>2. Fiddle with .proj file - copy-renames a whole new config file</h1> <p>Originally taken from <a href="http://icelava.net/forums/thread/2920.aspx" rel="noreferrer">here</a>. It's a custom MSBuild task that you can embed into Visual Studio <em>.proj</em> file. Copy and paste the following code into the project file</p> <pre><code>&lt;Target Name="AfterBuild"&gt; &lt;Delete Files="$(TargetDir)$(TargetFileName).config" /&gt; &lt;Copy SourceFiles="$(ProjectDir)\Config\App.$(Configuration).config" DestinationFiles="$(TargetDir)$(TargetFileName).config" /&gt; &lt;/Target&gt; </code></pre> <p>Now create a folder in the project called <code>Config</code> and add new files there: <em>App.Debug.config</em>, <em>App.Release.config</em> and so on. Now, depending on your configuration, Visual Studio will pick the config file from a <code>Config</code> folder, and copy-rename it into the output directory. So if you had <em>PatternPA.Test.Integration</em> project and a <em>Debug</em> config selected, in the output folder after the build you will find a <em>PatternPA.Test.Integration.dll.config</em> file which was copied from <code>Config\App.Debug.config</code> and renamed afterwards.</p> <p>These are some notes you can leave in the config files</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;configuration&gt; &lt;!-- This file is copied and renamed by the 'AfterBuild' MSBuild task --&gt; &lt;!-- Depending on the configuration the content of projectName.dll.config is fully substituted by the correspondent to build configuration file from the 'Config' directory. --&gt; &lt;/configuration&gt; </code></pre> <p>In Visual Studio you can have something like this</p> <p><img src="https://i.stack.imgur.com/uy2sh.png" alt="Project structure"></p> <h1>3. Use scripting files outside Visual Studio</h1> <p>Each build tool (like <a href="http://nant.sourceforge.net/" rel="noreferrer">NAnt</a>, <a href="http://msdn.microsoft.com/en-us/library/0k6kkbsd.aspx" rel="noreferrer">MSBuild</a>) will provide capabilities to transform config file depending on the configuration. This is useful if you build your solution on a build machine, where you need to have more control on what and how you prepare the product for release.</p> <p>For example you can use web publishing dll's task to transform any config file</p> <pre><code>&lt;UsingTask AssemblyFile="..\tools\build\Microsoft.Web.Publishing.Tasks.dll" TaskName="TransformXml"/&gt; &lt;PropertyGroup&gt; &lt;!-- Path to input config file --&gt; &lt;TransformInputFile&gt;path to app.config&lt;/TransformInputFile&gt; &lt;!-- Path to the transformation file --&gt; &lt;TransformFile&gt;path to app.$(Configuration).config&lt;/TransformFile&gt; &lt;!-- Path to outptu web config file --&gt; &lt;TransformOutputFile&gt;path to output project.dll.config&lt;/TransformOutputFile&gt; &lt;/PropertyGroup&gt; &lt;Target Name="transform"&gt; &lt;TransformXml Source="$(TransformInputFile)" Transform="$(TransformFile)" Destination="$(TransformOutputFile)" /&gt; &lt;/Target&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload