對於Visual Studio 2010基於Web的應用程序,咱們具備Config Transformation功能,經過該功能,咱們能夠爲不一樣的環境維護多個配置文件。 可是,對於Windows Services / WinForms或控制檯應用程序,App.Config文件沒法使用相同的功能。 web
這裏提供了一種解決方法: 將XDT魔術應用於App.Config 。 app
然而,它並不簡單,須要許多步驟。 有沒有更簡單的方法來實現相同的app.config文件? 編輯器
我編寫了一個很好的擴展來自動化app.config轉換,就像Web應用程序項目配置轉換中內置的同樣 ide
此擴展的最大優勢是您無需在全部構建計算機上安裝它 visual-studio
您能夠爲每一個配置使用單獨的配置文件,例如app.Debug.config,app.Release.config,而後在項目文件中使用配置變量: 測試
<PropertyGroup> <AppConfig>App.$(Configuration).config</AppConfig> </PropertyGroup>
而後,這將根據您正在構建的配置建立正確的ProjectName.exe.config文件。 ui
對如今彷佛處處發佈的解決方案稍做改進: this
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
受Oleg和其餘人在這個問題的啓發,我進一步採用瞭解決方案https://stackoverflow.com/a/5109530/2286801以啓用如下功能。 spa
此解決方案經過在MSBuild進程中首次引用app.config以前執行app.config轉換來工做。 它使用外部目標文件,以便更輕鬆地管理多個項目。 調試
與其餘解決方案相似的步驟。 我引用了相同的內容並將其包含在內,以便完整性和更容易比較。
0.在項目中添加一個名爲AppConfigTransformation.targets的新文件
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <!-- Transform the app config per project configuration.--> <PropertyGroup> <!-- This ensures compatibility across multiple versions of Visual Studio when using a solution file. However, when using MSBuild directly you may need to override this property to 11.0 or 12.0 accordingly as part of the MSBuild script, ie /p:VisualStudioVersion=11.0; See http://blogs.msdn.com/b/webdev/archive/2012/08/22/visual-studio-project-compatability-and-visualstudioversion.aspx --> <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion> </PropertyGroup> <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.targets" /> <Target Name="SetTransformAppConfigDestination" BeforeTargets="PrepareForBuild" Condition="exists('app.$(Configuration).config')"> <PropertyGroup> <!-- Force build process to use the transformed configuration file from now on. --> <AppConfig>$(IntermediateOutputPath)$(TargetFileName).config</AppConfig> </PropertyGroup> <Message Text="AppConfig transformation destination: = $(AppConfig)" /> </Target> <!-- Transform the app.config after the prepare for build completes. --> <Target Name="TransformAppConfig" AfterTargets="PrepareForBuild" Condition="exists('app.$(Configuration).config')"> <!-- Generate transformed app config in the intermediate directory --> <TransformXml Source="app.config" Destination="$(AppConfig)" Transform="app.$(Configuration).config" /> </Target> </Project>
1.爲項目添加每一個配置的XML文件。
一般,您將具備調試和發佈配置,所以將文件命名爲App.Debug.config和App.Release.config。 在個人項目中,我爲每種環境建立了一個配置,所以您可能想要嘗試一下。
2.卸載項目並打開.csproj文件進行編輯
Visual Studio容許您在編輯器中編輯.csproj - 您只須要先卸載項目。 而後右鍵單擊它並選擇Edit .csproj。
3.將App。*。配置文件綁定到主App.config
找到包含全部App.config和App。*。config引用的項目文件部分,並替換以下。 您會注意到咱們使用None而不是Content。
<ItemGroup> <None Include="app.config"/> <None Include="app.Production.config"> <DependentUpon>app.config</DependentUpon> </None> <None Include="app.QA.config"> <DependentUpon>app.config</DependentUpon> </None> <None Include="app.Development.config"> <DependentUpon>app.config</DependentUpon> </None> </ItemGroup>
4.激活變形魔法
在文件結束以後
<Import Project="$(MSBuildToolsPath)\\Microsoft.CSharp.targets" />在決賽以前
</Project>
插入如下XML:
<Import Project="AppConfigTransformation.targets" />
完成!
因此我最終採起了略微不一樣的方法。 我按照Dan的步驟完成了第3步,但添加了另外一個文件:App.Base.Config。 此文件包含每一個生成的App.Config中所需的配置設置。 而後我使用BeforeBuild(將Yuri添加到TransformXml中)將當前配置與Base配置轉換爲App.config。 而後,構建過程正常使用轉換後的App.config。 可是,有一種煩惱是你想要從源代碼控制中排除不斷變化的App.config,但其餘配置文件如今依賴於它。
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" /> <Target Name="BeforeBuild" Condition="exists('app.$(Configuration).config')"> <TransformXml Source="App.Base.config" Transform="App.$(Configuration).config" Destination="App.config" /> </Target>