Transform appSettings from external file after mer

2019-05-11 05:55发布

问题:

What I'm trying to do is transform one of appSettings which is in external file:

Here is 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>

After build in proper configuration which in my example is Debug there's only this:

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

but it should be:

<?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>

I tryed to make shared appSettings by 2 or more different project 1-st is WCF Service second ASP.NET MVC 4 Application

Edited:

I've tryed to move this file attribute to Web.Debug.config but it's as well doesn't work.

The question is:

How can i accomplish such thing?Is it even possible?

回答1:

Interesting. I have the same issue like yours. So now here is a workaround for your reference. Please open project file - XXX.csproj for example, ISWB.Test.Unit.csproj

add below section like this

<!-- 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>

Please notice the added section, you have to add it into cs project file in a TEXT editor manually. Please replace ISWB with yours. And then save it.

it should work well. Enjoy it!