从构建服务器预处理 T4 模板时出现 FileNotFoundException

FileNotFoundException when preprocessing T4 templates from build server

我正在尝试按照 Microsoft 文档中的说明进行操作 代码生成 在 Build Process 中,在我们的构建服务器上重建 T4 模板。什么时候 我构建时,模板生成失败并出现错误(完整的 MSBuild 输出 在这个问题的后面):

error MSB4018: The "TransformTemplates" task failed unexpectedly.

error MSB4018: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.TextTemplating.14.0, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

我创建了 a minimal project that reproduces the problem。剧本 build.cmd 只是执行 MSBuild。当我从我的 运行 build.cmd 工作站,构建成功。它只在构建服务器上失败。

我尝试过的事情:

我正在尝试避免安装 Visual Studio 2015,因为它非常漂亮 添加到我们的构建服务器和 Microsoft 文档的大量依赖项 明确表示这应该是可能的。

已将程序集复制到 C:\Program Files (x86)

我已将以下文件复制到构建服务器,如 代码中指定 生成文档(这些都在Program Files (x86)文件夹中,因为 那就是它们在我机器上的位置):

已添加到 GAC

我看到它尝试加载失败 Microsoft.VisualStudio.TextTemplating.14.0.dll,并且由于该程序集位于 我的工作站上的 GAC 我猜这可能是必需的,所以我尝试使用 GAC Manager 添加 Microsoft.VisualStudio.TextTemplating.14.0.dll 给 GAC,但错误是 一样。

完整的 MSBuild 输出

当我 运行 build.cmd 时,我得到以下输出:

D:\TTTest>"C:\Program Files (x86)\MSBuild.0\Bin\MSBuild.exe" ClassLibrary1.sln /t:Rebuild
Microsoft (R) Build Engine version 14.0.25420.1
Copyright (C) Microsoft Corporation. All rights reserved.

Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
Build started 10/20/2017 11:05:16 AM.
Project "D:\TTTest\ClassLibrary1.sln" on node 1 (Rebuild target(s)).
ValidateSolutionConfiguration:
  Building solution configuration "Debug|Any CPU".
Project "D:\TTTest\ClassLibrary1.sln" (1) is building "D:\TTTest\ClassLibrary1\ClassLibrary1.csproj" (2) on node 1 (Rebuild target(s)).
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: The "TransformTemplates" task failed unexpectedly. [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.TextTemplating.14.0, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: File name: 'Microsoft.VisualStudio.TextTemplating.14.0, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018:    at Microsoft.VisualStudio.TextTemplating.Sdk.Host.GenericTextTemplatingHost..ctor(IServiceProvider serviceProvider) [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018:    at Microsoft.VisualStudio.TextTemplating.Build.Tasks.TransformTemplatesBase.GetConfiguredTextTemplatingHost() [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018:    at Microsoft.VisualStudio.TextTemplating.Build.Tasks.TransformTemplatesBase.Execute() [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext() [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018:  [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: === Pre-bind state information === [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: LOG: DisplayName = Microsoft.VisualStudio.TextTemplating.14.0, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018:  (Fully-specified) [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: LOG: Appbase = file:///C:/Program Files (x86)/MSBuild/14.0/Bin/ [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: LOG: Initial PrivatePath = NULL [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: Calling assembly : Microsoft.VisualStudio.TextTemplating.Sdk.Host.14.0, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a. [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: === [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: LOG: This bind starts in LoadFrom load context. [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load(). [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: LOG: Using application configuration file: C:\Program Files (x86)\MSBuild.0\Bin\MSBuild.exe.Config [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: LOG: Using host configuration file:  [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config. [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: LOG: Post-policy reference: Microsoft.VisualStudio.TextTemplating.14.0, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: LOG: Attempting download of new URL file:///C:/Program Files (x86)/MSBuild/14.0/Bin/Microsoft.VisualStudio.TextTemplating.14.0.DLL. [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: LOG: Attempting download of new URL file:///C:/Program Files (x86)/MSBuild/14.0/Bin/Microsoft.VisualStudio.TextTemplating.14.0/Microsoft.VisualStudio.TextTemplating.14.0.DLL. [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: LOG: Attempting download of new URL file:///C:/Program Files (x86)/MSBuild/14.0/Bin/Microsoft.VisualStudio.TextTemplating.14.0.EXE. [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: LOG: Attempting download of new URL file:///C:/Program Files (x86)/MSBuild/14.0/Bin/Microsoft.VisualStudio.TextTemplating.14.0/Microsoft.VisualStudio.TextTemplating.14.0.EXE. [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: LOG: Attempting download of new URL file:///C:/Program Files (x86)/MSBuild/Microsoft/VisualStudio/v14.0/TextTemplating/Microsoft.VisualStudio.TextTemplating.14.0.DLL. [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: LOG: Attempting download of new URL file:///C:/Program Files (x86)/MSBuild/Microsoft/VisualStudio/v14.0/TextTemplating/Microsoft.VisualStudio.TextTemplating.14.0/Microsoft.VisualStudio.TextTemplating.14.0.DLL. [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: LOG: Attempting download of new URL file:///C:/Program Files (x86)/MSBuild/Microsoft/VisualStudio/v14.0/TextTemplating/Microsoft.VisualStudio.TextTemplating.14.0.EXE. [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018: LOG: Attempting download of new URL file:///C:/Program Files (x86)/MSBuild/Microsoft/VisualStudio/v14.0/TextTemplating/Microsoft.VisualStudio.TextTemplating.14.0/Microsoft.VisualStudio.TextTemplating.14.0.EXE. [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets(396,5): error MSB4018:  [D:\TTTest\ClassLibrary1\ClassLibrary1.csproj]
Done Building Project "D:\TTTest\ClassLibrary1\ClassLibrary1.csproj" (Rebuild target(s)) -- FAILED.

Done Building Project "D:\TTTest\ClassLibrary1.sln" (Rebuild target(s)) -- FAILED.


Build FAILED.

当我查看文件 Microsoft.TextTemplating.targets 时,发现它使用注册表项来查找包含 dlls:

的文件夹
 <PropertyGroup Condition=" $(IncludeDslT4Settings)=='true' ">
<!-- Path to VS\Common7\IDE -->
<!-- Check the 32bit location first ; if that is empty, use the 64bit location. -->
<VsIdePath>$(Registry:HKEY_LOCAL_MACHINE\Software\Microsoft\VisualStudio.0@InstallDir)</VsIdePath>
<VsIdePath Condition=" $(VsIdePath) == ''" >$(Registry:HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\VisualStudio.0@InstallDir)</VsIdePath>

<!-- Set the default location for the Dsl Designer install. -->
<DslDesignerInstallPath Condition=" $(DslDesignerInstallPath)=='' ">$(VsIdePath)Extensions\Microsoft\DSL SDK\Dsl Designer$(VisualStudioVersion)\</DslDesignerInstallPath>

<!-- Add the standard DSL templates folder -->
<IncludeFolders>$(IncludeFolders);$(DslDesignerInstallPath)TextTemplates\</IncludeFolders>

所以我必须在构建服务器上安装 Microsoft Visual Studio 2015 SDK 和 Microsoft Visual Studio 2015 Visualization & Modeling SDK 以注册 DLL。

但是,从 Visual Studio 2015 开始,您不再从下载中心安装 Visual Studio SDK。 它作为可选功能包含在 Visual Studio 设置 中。所以我们不安装Visual Studio就无法安装Visual StudioSDK。有关详细信息,请参阅 Visual Studio SDK

此外,Daniel's 有一篇关于 How to transform T4 templates on build without installing a Visual Studio SDK 的博客,您可以查看是否有帮助。

问题似乎是 .NET 找不到程序集 Microsoft.VisualStudio.TextTemplating.14.0.dll

因此,我没有像 Visual Studio 安装时那样将所有内容保存在单独的文件夹中,而是使用了以下文件:

  • C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating
    • Microsoft.VisualStudio.TextTemplating.Sdk.Host.14.0.dll
    • Microsoft.TextTemplating.Build.Tasks.dll
    • Microsoft.TextTemplating.targets
  • C:\Program Files (x86)\Microsoft Visual Studio 14.0\VSSDK\VisualStudioIntegration\Common\Assemblies\v4.0
    • Microsoft.VisualStudio.TextTemplating.14.0.dll
    • Microsoft.VisualStudio.TextTemplating.Interfaces.14.0.dll(多个文件)
    • Microsoft.VisualStudio.TextTemplating.VSHost.14.0.dll
  • C:\Program Files (x86)\Microsoft Visual Studio14.0\Common7\IDE\PublicAssemblies\
    • Microsoft.VisualStudio.TextTemplating.建模.14.0.dll

并将它们全部复制到 MSBuild 任务目录中:

  • C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating

这样做之后,.NET 能够找到它需要的程序集并转换模板。