Transform appSettings from external file after merge

做~自己de王妃 提交于 2019-12-07 12:44:26

问题


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!



来源:https://stackoverflow.com/questions/22509321/transform-appsettings-from-external-file-after-merge

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!