在部署 C#項目時轉換 App.config 配置文件


問題

部署項目時,常常需要根據不同的環境使用不同的配置文件。例如,在部署網站時可能希望禁用調試選項,並更改連接字符串以使其指向不同的數據庫。在創建 Web 項目時,Visual Studio 自動生成了 Web.configWeb.Debug.configWeb.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 時才完美解決配置文件轉換的問題。

正式做法

  1. 我們在項目中添加 App.configApp.Debug.configApp.Release.config 這3個配置文件。
  2. 打開項目所在目錄,用記事本或其他文本編輯器打開 .csproj 文件。
  3. 在 PropertyGroup 標簽下添加如下內容:

    <PropertyGroup>
    <ProjectConfigFileName>App.config</ProjectConfigFileName>
    </PropertyGroup>
  4. 在 ItemGroup 標簽中找到和 App.configApp.Debug.configApp.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>
  5. 在最后一個 Import 標簽后面添加:

    <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets" />
  6. 在 Import 標簽后面添加 Target 標簽:

    <Target Name="AfterBuild">
    <TransformXml Source="@(AppConfigWithTargetPath)" Transform="$(ProjectConfigTransformFileName)" Destination="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" />
    </Target>
  7. 切換到 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

(全文完)
 
轉轉:http://www.darrenfang.com/2016/01/transform-app-config-when-deploying-a-c-sharp-project/
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM