在部署 C#项目时转换 App.config 配置文件
問題
部署項目時,常常需要根據不同的環境使用不同的配置文件。例如,在部署網站時可能希望禁用調試選項,并更改連接字符串以使其指向不同的數據庫。在創建 Web 項目時,Visual Studio 自動生成了?Web.config、Web.Debug.config、Web.release.config這3個不同的配置文件,并提供了轉換工具,用于在部署項目時自動轉換配置文件內容。具體可以參考這2篇文章:如何:在部署 Web 應用程序項目時轉換 Web.config?和?用于 Web 應用程序項目部署的 Web.config 轉換語法?。
然而在其他項目類型中(如控制臺應用程序、Windows 服務),并沒有現成的配置文件的轉換功能。
臨時解決方案
準備2個配置文件:App.config?和?App.Release.config?,然后修改項目?.csproj?文件,更新?AfterBuild?生成事件:
| <Target Name="AfterBuild" Condition="'$(Configuration)' == 'Release' "><Delete Files="$(TargetDir)$(TargetFileName).config" /><Copy SourceFiles="$(ProjectDir)\app.$(Configuration).config" DestinationFiles="$(TargetDir)$(TargetFileName).config" /> </Target> | 
這樣在選擇 Release 配置時,執行生成操作會刪除?App.config?文件,然后用?App.Release.config文件替換。雖然這樣也可以實現根據環境來選擇配置文件,但是這種方法需要保證這2個配置文件內容保持同步,特別是要保證 assemblyBinding 標簽內容一致, 這個標簽的作用是程序集版本重定向,如果不一致會拋出 “未能加載文件或程序集” 這個異常。
直到找到這篇文章?Enable app.debug.config app.release.config?時才完美解決配置文件轉換的問題。
正式做法
在?PropertyGroup?標簽下添加如下內容:
| <PropertyGroup><ProjectConfigFileName>App.config</ProjectConfigFileName> </PropertyGroup> | 
在?ItemGroup?標簽中找到和?App.config、App.Debug.config、App.Release.config相關的項目,替換為
| <None Include="App.config" /> <None Include="App.Debug.config"><DependentUpon>App.config</DependentUpon> </None> <None Include="App.Release.config"><DependentUpon>App.config</DependentUpon> </None> | 
在最后一個?Import?標簽后面添加:
| <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets" /> | 
在?Import?標簽后面添加?Target?標簽:
| <Target Name="AfterBuild"><TransformXml Source="@(AppConfigWithTargetPath)" Transform="$(ProjectConfigTransformFileName)" Destination="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" /> </Target> | 
切換到 Visual Studio , 重新加載項目。
這時查看 Visual Studio 可以看到?App.config?的組織方式和?Web.config?一樣了。
App.config
現在就可以使用?用于 Web 應用程序項目部署的 Web.config 轉換語法?這篇文章中提到的轉換語法了。
例如需要替換?connectionStrings?,?App.config?有如下配置:
| <?xml version="1.0" encoding="utf-8" ?> <configuration><connectionStrings><add name="connString" connectionString="Server=debug;Database=test;Uid=root;Pwd=123456;CharSet=utf8;"providerName="MySql.Data.MySqlClient" /></connectionStrings> </configuration> | 
只需要修改?App.Release.config?為如下內容即可:
| <?xml version="1.0" encoding="utf-8"?><!-- 有關使用 web.config 轉換的詳細信息,請訪問 http://go.microsoft.com/fwlink/?LinkId=125889 --><configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"><connectionStrings><add name="connString"connectionString="Server=release;Database=test;Uid=root;Pwd=654321;CharSet=utf8;"xdt:Transform="SetAttributes" xdt:Locator="Match(name)" /></connectionStrings> </configuration> | 
這樣在選擇?Release?配置時,connectionStrings?會自動替換成?App.Release.config?中的值。查看?bin\Release?目錄下的 config 文件可以進行驗證。
完整代碼
| <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /><PropertyGroup><Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration><Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform><ProjectGuid>{8196CA4E-AD25-4F90-BB80-D27512BF4BD4}</ProjectGuid><OutputType>Exe</OutputType><AppDesignerFolder>Properties</AppDesignerFolder><RootNamespace>App.Config轉換</RootNamespace><AssemblyName>App.Config轉換</AssemblyName><TargetFrameworkVersion>v4.0</TargetFrameworkVersion><FileAlignment>512</FileAlignment></PropertyGroup><PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "><PlatformTarget>AnyCPU</PlatformTarget><DebugSymbols>true</DebugSymbols><DebugType>full</DebugType><Optimize>false</Optimize><OutputPath>bin\Debug\</OutputPath><DefineConstants>DEBUG;TRACE</DefineConstants><ErrorReport>prompt</ErrorReport><WarningLevel>4</WarningLevel></PropertyGroup><PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "><PlatformTarget>AnyCPU</PlatformTarget><DebugType>pdbonly</DebugType><Optimize>true</Optimize><OutputPath>bin\Release\</OutputPath><DefineConstants>TRACE</DefineConstants><ErrorReport>prompt</ErrorReport><WarningLevel>4</WarningLevel></PropertyGroup><PropertyGroup><ProjectConfigFileName>App.config</ProjectConfigFileName></PropertyGroup><ItemGroup><Reference Include="System" /><Reference Include="System.configuration" /><Reference Include="System.Core" /><Reference Include="System.Xml.Linq" /><Reference Include="System.Data.DataSetExtensions" /><Reference Include="Microsoft.CSharp" /><Reference Include="System.Data" /><Reference Include="System.Xml" /></ItemGroup><ItemGroup><Compile Include="Program.cs" /><Compile Include="Properties\AssemblyInfo.cs" /></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><SubType>Designer</SubType></None></ItemGroup><Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /><Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets" /><!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets.<Target Name="BeforeBuild"></Target><Target Name="AfterBuild"></Target>--><Target Name="AfterBuild"><TransformXml Source="@(AppConfigWithTargetPath)" Transform="$(ProjectConfigTransformFileName)" Destination="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" /></Target> </Project> | 
示例項目下載:
App.Config轉換.zip
參考鏈接
如何:在部署 Web 應用程序項目時轉換 Web.config
用于 Web 應用程序項目部署的 Web.config 轉換語法
Enable app.debug.config app.release.config
總結
以上是生活随笔為你收集整理的在部署 C#项目时转换 App.config 配置文件的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 中信信用卡账单分期可以提前还款吗
- 下一篇: 中信大众点评联名信用卡怎么提额
