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.

Auto transform App.config

App.config add Stage setting,value=Debug
image


add two files: App.Release.config and App.Debug.config
image

in App.Release.config add Stage setting,value=Release
image

Debug.config keep empty,the file is required for transform checking if exists.

project.csproj add msbuild settings as below

1.PropertyGroup
<PropertyGroup> <ProjectConfigFileName>App.config</ProjectConfigFileName> </PropertyGroup>
2.ItemGroup
<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>
3.Import
<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
image

after saving,these files will be presenting by the hierarchy

image

Testing Build

To switch Release then compile,the value of Stage has changed "Release"
image

transform connection string with the outer file


In App.config add  connectionStrings setting
image

add file "Connection.config"
image

Connection.config file property set "Copy always"
image

App.Release.config set connectionStrings point to Connection.config
image

Testing Build

To switch Release then compile.the configSource has referred to outer file.
image

Source Code:  https://github.com/kimx/AppConfigTransformLab

References

http://demo.tc/post/775
https://msdn.microsoft.com/en-us/library/ms254494(v=vs.110).aspx

Popular posts from this blog

VS2017-Build fail-FindDependenciesOfExternallyResolvedReferences

SQL Server pass parameter to Procedure

Jenkins- PowerShell include global config