Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

c# - web.config transform from web.template.xml not working

I'm trying to get web.config transformations working as described here. We've used this method on other projects and it works without issue, but not on this new project.

Here's what I've tried testing without success

  • Changing name of wpp.targets file in case I got the project name wrong. I know the current one I'm using works since it's the only one that causes web.config to be rebuilt from web.template.xml this transform works. Only the sub templates don't work.
  • Tried with xdt:Locator="Match(name)"
  • Tried .config extension vs .xml, our other projects where this works use .xml
  • Configuration manager is set to use the "Test" configuration for the project I'm working on.
  • web.template.Test.xml has xdt:Transform="Replace" for the section I want to replace
  • web.template.xml has the placeholder
  • Tried removing the "CopyWebTemplateConfig" section from wpp.targets as suggested on the stack question linked below. Our other projects have this and the "PropertyGroup" section commented out and I've tried both combinations.

I've read through the above link multiple times and this related stack question, but can't see what the problem is.

Note The publish transform does work in a way. It creates a web.template.xml file that contains the values from web.template.Test.xml, but does not create a web.config.xml as the wpp.targets instructs. So this is more of an issue with getting the build transform working it seems.

Anyone have an idea of what's missing?

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 output

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had installed StyleCop and that was doing the overwrite for me.

So I uninstalled it and the issue was resolved.

Funny is that I re-installed the StyleCop and the transform was still working!

Also at some points I noticed that I should remove the CopyWebTemplateConfig target section as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...