We're using an AfterBuild
target in the vbproj
to replace the config file depending on the selected configuration :
<Target Name="AfterBuild" Condition="'$(Configuration)' != 'Release'">
<Delete Files="$(TargetDir)$(TargetFileName).config" />
<Copy SourceFiles="$(ProjectDir)$(Configuration).config" DestinationFiles="$(TargetDir)$(TargetFileName).config" />
</Target>
As an example, let's say we have 3 configurations : Debug, Test, Release. Debug is the local debugging configuration. Test is a pre-production environment used for user-acceptance tests. Release is our production environment.
In the App.config
file, we store the configuration for our Release environment. In the Debug.config
file, we store the configuration for our local debugging needs. In the Test.config
file, we store the configuration for our user-acceptance environment.
The goal of the AfterBuild
target is to replace the Release configuration (App.config
) when building/executing with the Debug configuration (Debug.config
) or the Test configuration (Test.config
).
Everything works as intended when we publish the application (Release, App.config
) or if we build the application and launch the bin\<appname>.exe
(Debug or Test).
However, if we Start the application from Visual Studio, with the Visual Studio hosting process, the correct configuration is copied to bin\<appname>.exe.config
, but it seems that Visual Studio doesn't copy the correct configuration to bin\<appname>.vshost.exe.config
. We tried cleaning the solution, performing a Rebuild before debugging, manually deleting the bin\<appname>.vshost.exe.config
file before launching, but it seems that the hosting process always copies the configuration from the default App.config
file. The same problem happens whether we try to Start with the Debug configuration or the Test configuration.
To add to the confusion, we created multiple test projects, using the same AfterBuild
target, and some of them work correctly, others don't. All projects are using .Net Framework 4.5.1, but we also reproduced the problem with .Net 4.5. It doesn't seem to be caused by the project type, since we're able to reproduce the issue with a Console Application as well as a Windows Forms Application.
What could be causing the problem?
Alternatively, could we use another solution to manage the configuration for each of our environment?
Notes
- We use the default App.config file for our Release environment because ClickOnce doesn't seem to support AfterBuild targets.
- We use Visual Studio Express 2013 for Windows Desktop, so we can't use any plugin like SlowCheetah.