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

問題

部署項目時,經常須要根據不一樣的環境使用不一樣的配置文件。例如,在部署網站時可能但願禁用調試選項,並更改鏈接字符串以使其指向不一樣的數據庫。在建立 Web 項目時,Visual Studio 自動生成了 Web.configWeb.Debug.configWeb.release.config這3個不一樣的配置文件,並提供了轉換工具,用於在部署項目時自動轉換配置文件內容。具體能夠參考這2篇文章:如何:在部署 Web 應用程序項目時轉換 Web.config 和 用於 Web 應用程序項目部署的 Web.config 轉換語法 。web

然而在其餘項目類型中(如控制檯應用程序、Windows 服務),並無現成的配置文件的轉換功能。數據庫

臨時解決方案

準備2個配置文件:App.config 和 App.Release.config ,而後修改項目 .csproj 文件,更新 AfterBuild 生成事件:app

<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 時才完美解決配置文件轉換的問題。ide

正式作法

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

    <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 標籤:ui

    <Target Name="AfterBuild">
    <TransformXml Source="@(AppConfigWithTargetPath)" Transform="$(ProjectConfigTransformFileName)" Destination="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" />
    </Target>
  7. 切換到 Visual Studio , 從新加載項目。spa

這時查看 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/
相關文章
相關標籤/搜索