问题

部署项目时,常常需要根据不同的环境使用不同的配置文件。例如,在部署网站时可能希望禁用调试选项,并更改连接字符串以使其指向不同的数据库。在创建 web 项目时,visual studio 自动生成了 web.configweb.debug.configweb.release.config这3个不同的配置文件,并提供了转换工具,用于在部署项目时自动转换配置文件内容。具体可以参考这2篇文章:如何:在部署 web 应用程序项目时转换 web.config 和 用于 web 应用程序项目部署的 web.config 转换语法 。

然而在其他项目类型中(如控制台应用程序、windows 服务),并没有现成的配置文件的转换功能。

 

  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 , 重新加载项目。

8.这时查看 visual studio 可以看到 app.config 的组织方式和 web.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>

转自:在部署 c#项目时转换 app.config 配置文件