8

我的 Web.config 文件中有下一个配置

<Target Name="UpdateWebConfigForProjectsBeforeRun">
    <ItemGroup>
      <FilesToTransofm Include="ProjectsDeployBin\Web.*.$(Configuration).config"/>      
    </ItemGroup>    
    <Message Text="Transform file: %(FilesToTransofm.Identity)" />
    <TransformXml Source="web.config"
                  Transform="%(FilesToTransofm.Identity)"
                  Destination="web.config" />
  </Target>

我正在尝试从 ProjectsDeployBin 目录中获取所有配置并将每个文件应用于主 web.config。

在第一次转换后,主 web.config 被 msbuild 锁定。

那么我该如何解决这个问题呢?还有其他方法可以通过文件集合来转换我的 web.config 吗?谢谢。

4

1 回答 1

8

As you've noticed, the TransformXml task shipped with Visual Studio 2010 has a bug that leaves the source file locked.

To work around that, you can make a temporary copy of your source file before each transformation. Since you'll then be executing multiple tasks for each transform file (copy and transform), you'll need to switch to Target Batching instead of Task Batching.

Example:

<ItemGroup>
  <FilesToTransofm Include="ProjectsDeployBin\Web.*.$(Configuration).config"/>      
</ItemGroup>

<Target Name="UpdateWebConfigForProjectsBeforeRun"
        Inputs="@(FilesToTransofm)"
        Outputs="%(Identity).AlwaysRun">
  <Message Text="Transform file: %(FilesToTransofm.Identity)" />
  <Copy SourceFiles="web.config"
        DestinationFiles="web.pre-%(FilesToTransofm.Filename).temp.config" />
  <TransformXml Source="web.pre-%(FilesToTransofm.Filename).temp.config"
                Transform="%(FilesToTransofm.Identity)"
                Destination="web.config" />
</Target>

From a quick test, it looks like this bug is fixed in Visual Studio 2012, but I'm not able to find a reference / source that documents that, and the original Connect bug isn't viewable anymore.

于 2012-10-19T01:23:42.993 回答