web.template.xml的web.config转换不起作用

本文关键字:web 转换 不起作用 config template xml | 更新日期: 2024-09-19 16:33:38

我正在尝试让web.config转换按此处所述工作。我们已经在其他项目中使用了这种方法,它可以毫无问题地工作,但在这个新项目中没有。

以下是我尝试测试但没有成功的

  • 更改wpp.targets文件的名称,以防我弄错了项目名称。我知道我目前使用的是有效的,因为它是唯一一个导致web.config从web.template.xml重建的转换。只有子模板不起作用
  • 尝试使用xdt:Locator="匹配(名称)"
  • 尝试了.config扩展与.xml,我们的其他项目使用了.xml
  • 配置管理器被设置为对我正在进行的项目使用"测试"配置
  • web.template.Test.xml对我要替换的部分具有xdt:Transform="Replace"
  • web.template.xml具有占位符
  • 根据下面链接的堆栈问题的建议,尝试从wpp.target中删除"CopyWebTemplateConfig"部分。我们的其他项目都有这一点,"PropertyGroup"部分对此进行了评论,我已经尝试了这两种组合

我已经多次阅读了上面的链接和这个相关的堆栈问题,但看不出问题出在哪里

注意发布转换确实在某种程度上起作用。它创建一个web.template.xml文件,该文件包含web.template.Test.xml中的值,但不会按照wpp.target的指示创建web.config.xml。因此,这更像是一个让构建转换发挥作用的问题。

有人知道缺了什么吗?

wpp.targets

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- Make sure web.config will be there even for package/publish -->
  <Target Name="CopyWebTemplateConfig" BeforeTargets="Build">
    <Copy SourceFiles="web.template.xml"
          DestinationFiles="web.config"/>
  </Target>
  <PropertyGroup>
    <PrepareForRunDependsOn>
      $(PrepareForRunDependsOn);
      UpdateWebConfigBeforeRun;
    </PrepareForRunDependsOn>
  </PropertyGroup>
  <!-- This target will run right before you run your app in Visual Studio -->
  <Target Name="UpdateWebConfigBeforeRun">
    <Message Text="Configuration: $(Configuration): Web.template.$(Configuration).xml"/>
    <TransformXml Source="web.template.xml"
              Transform="web.template.$(Configuration).xml"
              Destination="web.config" />
  </Target>
  <!-- Exclude the config template files from the created package -->
  <Target Name="ExcludeCustomConfigTransformFiles" BeforeTargets="ExcludeFilesFromPackage">
    <ItemGroup>
      <ExcludeFromPackageFiles Include="web.template.xml;web.template.*.xml"/>
    </ItemGroup>
    <Message Text="ExcludeFromPackageFiles: @(ExcludeFromPackageFiles)" Importance="high"/>
  </Target>
</Project>

web.template.xml

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=152368
  -->
<configuration>
  <configSections>           
    <sectionGroup name="TestSettings"></sectionGroup>
    ....
  </configSections>
    ....
  <TestSettings>
  </TestSettings>
   ....
</configuration>

web.template.Test.xml

<?xml version="1.0"?>
<!-- For more information on using transformations 
     see the web.config examples at http://go.microsoft.com/fwlink/?LinkId=214134. -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <TestSettings xdt:Transform="Replace">
    ...
  </TestSettings>
</configuration>

MSBuild输出

Target "UpdateWebConfigBeforeRun: (TargetId:143)" in file "C:'...'Project.wpp.targets" from project "C:'...'Project.csproj" (target "PrepareForRun" depends on it):
Task "Message" (TaskId:93)
  Configuration: Test: Web.template.Test.xml (TaskId:93)
Done executing task "Message". (TaskId:93)
Task "TransformXml" (TaskId:94)
  Transforming Source File: Web.template.xml (TaskId:94)
    Applying Transform File: Web.template.Test.xml (TaskId:94)
    Executing Replace (transform line 5, 18) (TaskId:94)
      on /configuration/TestSettings (TaskId:94)
      Applying to 'TestSettings' element (source line 121, 4) (TaskId:94)
      Replaced 'TestSettings' element (TaskId:94)
    Done executing Replace (TaskId:94)
    Output File: web.config (TaskId:94)
  Transformation succeeded (TaskId:94)
Done executing task "TransformXml". (TaskId:94)
Done building target "UpdateWebConfigBeforeRun" in project "Project.csproj".: (TargetId:143)

web.template.xml的web.config转换不起作用

我安装了StyleCop,它正在为我进行覆盖。

所以我卸载了它,问题得到了解决。

有趣的是,我重新安装了StyleCop,转换仍然有效!

此外,在某些方面,我注意到我也应该删除CopyWebTemplateConfig目标部分。

我已经找到了解决问题的方法,但不确定原因是什么,所以不确定这是否能在其他情况下解决问题。

我查看了MSBuild诊断的输出,注意到最后还有另一个部分将web.template复制到web.config。请注意,这是在UpdateWebConfigBeforeRun目标已经运行并从子模板转换文件更新到web.config之后。看起来最后一步是用我想要的转换覆盖web.config。

我不确定最后一组复制指令是从哪里来的,所以我在电脑上搜索了所有文件,寻找其他wpp.target文件。我在Slow Cheetah的扩展文件夹中找到了另一个,并看到顶部的某个部分将属性"transformOnBuild"设置为false。

我认为与SlowCheetah有冲突,于是卸载了它,转换开始按预期工作。这仍然有点奇怪,因为其他解决方案都启用了SlowCheetah。心血来潮,我重新安装了SlowCheetah,改造继续按预期进行。

所以我的解决方案最终是重新安装SlowCheetah。我仍然不清楚这个问题的原因是什么,所以如果有其他人发布了这个问题的答案,我会给他们赏金。

上周我也遇到过类似的问题。事实证明,无论何时在VS 2010中向解决方案添加项目,都不会始终应用正确的项目配置。因此,您认为您有一个活动的配置,但另一个配置实际上在该项目中处于活动状态,因此您所期望的转换没有得到应用。

查看此问题最后一条注释中的步骤:自定义解决方案配置未显示在Visual Studio 2010 中

"我知道我使用的当前模板有效,因为它是唯一一个导致从web.template.xml重建web.config的模板。此转换有效。只有子模板不起作用。"

这是否意味着,转换是有效的,但TestSettings部分本身并没有被转换?

如果详细程度设置为诊断/详细,是否可以与msbuild共享生成输出?

我写了一篇关于这个主题的博客文章。我每天都在我们的网络应用程序中使用它。我写博客是因为slowcheetah的功能还没有准备好。http://www.locktar.nl/general/use-config-transforms-when-debugging-your-web-application/