Web.config转换——缺失的手册

7
你可以在这里查看那里阅读有关web.config转换的文档,但似乎没有人讨论以下两个难题:
  1. 如何在ConditionXPath转换中执行变量替换,以及...
  2. Locator可以被有意义地嵌套在Transform中吗?

让我举个例子,说明这两个选项的好处。假设我有以下内容:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
      <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
      <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>

假设我想要完全删除与xpath //runtime/assemblyBinding/dependentAssembly[assemblyIdentity@name='System.Web.Mvc']匹配的dependentAssembly节点及其子节点。为了做到这一点,我可能需要像这样的代码:
<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity 
          name="System.Web.Mvc" 
          xdt:Remove 
          xdt:Locator="Condition(..[*@name=$name])" 
      />
    </dependentAssembly>
  </assemblyBinding>
</runtime>

显然,我基于xpath variable concepts编造了@name=$name的语法,但这个例子展示了我为什么需要这个功能。这个功能被支持吗?我需要如何调整我的语法来利用它?我可以放入一个字符串文字,但我只是想知道是否可能。

另一种我试图删除dependentAssembly节点的方法是这样的:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1" xdt:Transform="Remove">
    <dependentAssembly>
      <assemblyIdentity name="System.Web.Mvc" xdt:Locator="Match(name)" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>

请注意,Transform 在祖先节点上,而定位器在叶节点上。上述内容是否合法?其想法是仅删除具有内部定位器匹配的dependantAssembly节点。
除了这两种方法之外,您如何删除目标dependantAssembly及其所有子节点?
3个回答

9
问题在于assemblyBinding标签上的namespace属性。如果删除AspNetHelper参考,则以下内容对我有效:
<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly xdt:Transform="Remove" 
                           xdt:Locator="Condition(./_defaultNamespace:assemblyIdentity/@name='Microsoft.VisualStudio.Enterprise.AspNetHelper')">
        </dependentAssembly>
    </assemblyBinding>
</runtime>

这个有效,并且应该被标记为这个问题的答案。 - sebastiaan

7

@Thommy的解决方案对我很有用,而@LifeintheGrid的解决方案使用了我想要删除的实际程序集,所以我将两者结合并简化为:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly xdt:Transform="RemoveAll"
                           xdt:Locator="Condition(starts-with(./_defaultNamespace:assemblyIdentity/@name,'Microsoft.VisualStudio.QualityTools'))">
        </dependentAssembly>
    </assemblyBinding>
</runtime>

3
这段代码对我很有效。我将transform移动到了dependentAssembly节点。
<runtime>
  <assemblyBinding>
    <!-- ending /dependentAssembly is required or tranforms fail -->
    <dependentAssembly xdt:Transform="Remove" xdt:Locator="Condition(assemblyIdentity/@name='Microsoft.VisualStudio.QualityTools.HostAdapters.ASPNETAdapter')"  ></dependentAssembly>
    <dependentAssembly xdt:Transform="Remove" xdt:Locator="Condition(assemblyIdentity/@name='Microsoft.VisualStudio.QualityTools.Common')"  ></dependentAssembly>
    <dependentAssembly xdt:Transform="Remove" xdt:Locator="Condition(assemblyIdentity/@name='Microsoft.VisualStudio.QualityTools.ExecutionCommon')"></dependentAssembly>
    <dependentAssembly xdt:Transform="Remove" xdt:Locator="Condition(assemblyIdentity/@name='Microsoft.VisualStudio.QualityTools.Resource')"  ></dependentAssembly>
  </assemblyBinding>
</runtime>  

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接