Console Program app.config trasform
I have a console app to some of the background tasks.I got some troubles with switch QAS stage and production stage.For that, I need to replace the app setting for difference machine.Therefore I write some codes to do thus to got my purpose.Today I saw an article to introduce to do that.I realize that I have done the stupid thing to deal with that thing.This article is for my regretted note.
add two files: App.Release.config and App.Debug.config
in App.Release.config add Stage setting,value=Release
Debug.config keep empty,the file is required for transform checking if exists.
Reference as below image
after saving,these files will be presenting by the hierarchy
In App.config add connectionStrings setting
add file "Connection.config"
Connection.config file property set "Copy always"
App.Release.config set connectionStrings point to Connection.config
Source Code: https://github.com/kimx/AppConfigTransformLab
https://msdn.microsoft.com/en-us/library/ms254494(v=vs.110).aspx
Auto transform App.config
App.config add Stage setting,value=Debugadd two files: App.Release.config and App.Debug.config
in App.Release.config add Stage setting,value=Release
Debug.config keep empty,the file is required for transform checking if exists.
project.csproj add msbuild settings as below
1.PropertyGroup2.ItemGroup<PropertyGroup> <ProjectConfigFileName>App.config</ProjectConfigFileName> </PropertyGroup>
3.Import<ItemGroup> <None Include="App.config" /> <None Include="App.Debug.config"> <DependentUpon>App.config</DependentUpon> </None> <None Include="App.Release.config"> <DependentUpon>App.config</DependentUpon> </None> </ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.Publishing.targets" /> <Target Name="AfterBuild"> <TransformXml Source="@(AppConfigWithTargetPath)" Transform="$(ProjectConfigTransformFileName)" Destination="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" /> </Target>
Reference as below image
after saving,these files will be presenting by the hierarchy
Testing Build
To switch Release then compile,the value of Stage has changed "Release"transform connection string with the outer file
In App.config add connectionStrings setting
add file "Connection.config"
Connection.config file property set "Copy always"
App.Release.config set connectionStrings point to Connection.config
Testing Build
To switch Release then compile.the configSource has referred to outer file.Source Code: https://github.com/kimx/AppConfigTransformLab
References
http://demo.tc/post/775https://msdn.microsoft.com/en-us/library/ms254494(v=vs.110).aspx