NuGet包转换配置转换文件

时间:2015-07-12 12:38:13

标签: nuget web.config-transform xdt-transform

有没有办法让NuGet包转换配置转换文件?例如,当我希望我的NuGet包编辑web.config文件时,我创建了一个web.config.install.xdt文件。但是如果我想让我的NuGet包编辑web.config.debug文件呢?

我尝试制作一个web.config.debug.install.xdt文件,但遇到了一个问题:我无法进行转换以插入属于xdt转换属性的属性。类似的东西:

<?xml version="1.0" encoding="utf-8" ?>
<configuration  xmlns:xdt1="http://schemas.microsoft.com/XML-Document-Transform">

  <system.serviceModel >
    <client xdt1:Transform="Insert">
      <endpoint address="http://blah.blah" binding="basicHttpBinding" contract="Test.Contract" 
                name="TestWs" xdt:Transform="Replace" xdt:Locator="Match(name)"/>
    </client>
  </system.serviceModel>

</configuration>

(我尝试更改xdt的命名空间,但这也没有帮助。)

1 个答案:

答案 0 :(得分:1)

虽然这可能不是最好的答案,但当我发现自己处于这种情况时,它确实为我完成了工作:

使用“旧”方法进行变换,而不是xdt方式。

https://docs.nuget.org/create/Transforming-Configuration-Files-Using-dotTransform-Files.md

这似乎运行良好,只需确保相应的xmlns属性位于.transform文件中。

例如,如果您想要转换目前看起来像这样的web.qa.config文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
      <appSettings>
            <add key="Tier" value="qa" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
      </appSettings>
 </configuration>

您可以添加元素:

<add key="RedirectUri" value="yourRedirectUriForQA" xdt:Transform="Replace" />

将以下web.qa.config.transform文件添加到您的Nuget包中:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <appSettings>
         <add key="RedirectUri" value="yourRedirectUriForQA" xdt:Transform="Replace" />
    </appSettings>
</configuration>

请确保将其添加到.nuspec文件中,以便在打包时将其拾取。