【问题标题】:Transform appSettings from external file after merge合并后从外部文件转换 appSettings
【发布时间】:2015-03-19 14:42:35
【问题描述】:

我正在尝试做的是transform 之一,appSettings 在外部文件中:

这里是 external.config

<?xml version="1.0"?>
    <appSettings>
    <add key="SomeKey" value="some value" />
    </appSettings>

Web.config

<?xml version="1.0"?>
    <configuration>
        <appSettings file="..\..\external.config">
            <add key="SomeKey1" value="some value 1" />
        </appSettings>
    </configuration>

Web.Debug.config

<?xml version="1.0"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
        <appSettings>
            <add key="SomeKey" value="some changed value"xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
       </appSettings>
    </configuration>

在构建正确的configuration(在我的示例中为Debug)之后,只有这个:

<?xml version="1.0"?>
    <configuration>
        <appSettings file="..\..\external.config">
            <add key="SomeKey1" value="some value 1" />
        </appSettings>
    </configuration>

但应该是:

<?xml version="1.0"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
        <appSettings>
            <add key="SomeKey1" value="some value 1" />
            <add key="SomeKey" value="some changed value"/>
       </appSettings>
    </configuration>

我尝试将appSettings 共享为 2 个或更多不同的 project 1-st 是 WCF Service 第二个 ASP.NET MVC 4 Application

已编辑:

我已尝试将此 file attribute 移动到 Web.Debug.config 但它也不起作用。

问题是:

我怎样才能完成这样的事情?有可能吗?

【问题讨论】:

  • 您在上面找到的任何解决方案?谢谢

标签: asp.net-mvc wcf-configuration web.config-transform


【解决方案1】:

有趣。我有和你一样的问题。所以现在这里有一个解决方法供您参考。 请打开项目文件 - XXX.csproj 例如,ISWB.Test.Unit.csproj

像这样添加下面的部分

<!-- Rock Add here, 2015.03.19 enable the external config transformation -->
  <Target Name="BeforeCompile" Condition="Exists('ISWB.$(Configuration).config')">
    <!--Generate transformed app config in the intermediate directory-->
    <TransformXml Source="ISWB.config" Destination="$(IntermediateOutputPath)ISWB.config" Transform="ISWB.$(Configuration).config" />
    <!--Force build process to use the transformed configuration file from now on.-->
    <ItemGroup>
      <AppConfigWithTargetPath Remove="ISWB.config" />
      <AppConfigWithTargetPath Include="$(IntermediateOutputPath)ISWB.config">
        <TargetPath>ISWB.config</TargetPath>
      </AppConfigWithTargetPath>
    </ItemGroup>
  </Target>

  <Target Name="AfterCompile" Condition="Exists('app.$(Configuration).config')">
    <!--Generate transformed app config in the intermediate directory-->
    <TransformXml Source="app.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="app.$(Configuration).config" />
    <!--Force build process to use the transformed configuration file from now on.-->
    <ItemGroup>
      <AppConfigWithTargetPath Remove="app.config" />
      <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
        <TargetPath>$(TargetFileName).config</TargetPath>
      </AppConfigWithTargetPath>
    </ItemGroup>
  </Target>

请注意添加的部分,您必须手动将其添加到文本编辑器中的 cs 项目文件中。 请用您的替换 ISWB。然后保存。

它应该运作良好。 尽情享受吧!

【讨论】:

    猜你喜欢
    • 2013-09-20
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 2012-10-06
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    相关资源
    最近更新 更多