msbuild 使用了错误的程序集名称

msbuild uses the wrong assembly name

我在使用从未遇到过问题的解决方案中构建一些 C# 项目时遇到了问题。构建失败,并显示缺少元数据文件的错误。诊断级别的错误描述和 msbuild 日志输出显示了一些令人惊讶的进展。我遇到这个问题的项目之一叫做 WpfControlLibrary。它在解决方案中引用了以下项目:

Helpers
HunAlmex.Kioszk.Common
HunAlmex.Kioszk.Communication.ImportedServiceContracts
HunAlmex.Kioszk.Data

解决方案文件夹是 "D:\Entegro\TFS\Volánbusz TVM\Bugfixes 2018-06"

构建失败错误如下:

Error CS0006 Metadata file 'D:\Entegro\TFS\Volánbusz TVM\Bugfixes 2018-06\HunAlmex.Kioszk.Data\bin\Debug\WpfControlLibrary.dll' could not be found (in project WpfControlLibrary; file CSC)

因此,当我构建 WpfControlLibrary 项目时,CSC 正在 [=17] 项目之一的 bin\debug 文件夹中寻找以该项目命名的 dll =] 项目正在引用。

构建日志显示上述所有四个项目引用都是这种情况。在看似正确地构建了这些项目之后,该构建正在他们的 bin\debug 文件夹中寻找一个名为 WpfControlLibrary.dll 的 dll(这些项目是使用正确命名的 dll 和 pdb 文件构建的)。构建错误可能只包含一个丢失的 dll,因为它在第一个错误时失败。

但是,如果我构建整个解决方案,并且查看 Helpers 项目的 bin\debug 文件夹,我会发现项目已正确构建,然后在稍后的构建过程中, Helpers.dll 文件消失,出现一个名为 WpfControlLibrary.dll 的文件(和 pdb)。最后构建失败,因为没有找到Helpers.dll.

WpfControlLibrary 项目引用的项目未引用 WpfControlLibrary

以上结果是由VS Enterprise 2017 version 15.8.2msbuild version 15.8.168.64424产生的。我最近更新了 VS,在其他人身上工作了一段时间后又回到了这个解决方案,所以我不知道更新是否破坏了它。

我尝试在同一台计算机上使用 VS Community 2017 version 15.2msbuild version 15.1.1012.6693 构建有问题的项目,这样可以很好地构建它们。

该项目也可以在其他计算机上使用各种其他版本的 VS 和 msbuild 正常构建。

我对 msbuild 了解不多,但我比较了企业版和社区版的构建日志,前者的 dll 名称似乎在以下几行之后变坏了:

Task Parameter:
1>      Properties=
1>          Configuration=Debug
1>          Platform=AnyCPU (TaskId:12)
1>  Global Properties: (TaskId:12)
1>    Configuration=Debug (TaskId:12)
1>    Platform=AnyCPU (TaskId:12)
1>  Removing Properties for project "..\Helpers\Helpers.csproj": (TaskId:12)
1>    TargetFramework (TaskId:12)
1>  Removing Properties for project "..\HunAlmex.Kioszk.Common\HunAlmex.Kioszk.Common.csproj": (TaskId:12)
1>    TargetFramework (TaskId:12)
1>  Removing Properties for project "..\HunAlmex.Kioszk.Communication.ImportedServiceContracts\HunAlmex.Kioszk.Communication.ImportedServiceContracts.csproj": (TaskId:12)
1>    TargetFramework (TaskId:12)
1>  Removing Properties for project "..\HunAlmex.Kioszk.Data\HunAlmex.Kioszk.Data.csproj": (TaskId:12)
1>    TargetFramework (TaskId:12)

社区构建日志中缺少这些,但可能还有很多其他差​​异。

我不确定这是 VS 的错。可能是构建过程采用构建目标的全局 msbuild 文件损坏了。我尝试修复 VS,但没有任何区别。

我正在link查看下面的构建日志。

我有一种预感,我只能通过删除并重新安装或修复已安装的 .NET Framework 版本来解决此问题。我可以直接从上下文菜单中的 Control Panel / Programs and Features / <.NET Framework version> / Repair 执行此操作吗?

https://drive.google.com/file/d/1NqLrzkmQhYSoYQxoja76NQp484gWNTHL/view?usp=sharing

下载压缩的诊断级构建日志

更新

构建日志引用每个项目的 Microsoft.Common.CurrentVersion.targets 文件。例如:

1>Target "GetTargetPathWithTargetPlatformMoniker: (TargetId:19)" in file "C:\Program Files (x86)\Microsoft Visual Studio17\Enterprise\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets" from project "D:\Entegro\TFS\Volánbusz TVM\Bugfixes 2018-06\Helpers\Helpers.csproj" (target "GetTargetPath" depends on it):
1>Added Item(s): 
1>    TargetPathWithTargetPlatformMoniker=
1>        D:\Entegro\TFS\Volánbusz TVM\Bugfixes 2018-06\Helpers\bin\Debug\Helpers.dll
1>                CopyUpToDateMarker=D:\Entegro\TFS\Volánbusz TVM\Bugfixes 2018-06\Helpers\obj\Debug\Helpers.csproj.CopyComplete
1>                TargetFrameworkIdentifier=.NETFramework
1>                TargetFrameworkVersion=4.0
1>                TargetPlatformIdentifier=Windows
1>                TargetPlatformMoniker=Windows,Version=7.0
1>Done building target "GetTargetPathWithTargetPlatformMoniker" in project "Helpers.csproj".: (TargetId:19)

如果我用 VS Community 构建日志引用的文件替换该文件并构建项目,它仍然会出现相同的错误。但是,如果我清理该项目,然后进行构建,它会毫无错误地构建它。对于 VS 和 msbuild 版本,请参见上文。我link下面的两个文件。文件名显示它们是哪个版本。

企业:https://drive.google.com/open?id=1MPnAVQxMtjTcuy39pzmLJolIroBaDgMt

社区:https://drive.google.com/open?id=19Jn8UkRaJ5oAFXreXI4IpRKmGrnjtGQ8

我将尝试使用显示此行为的项目编译一个小型解决方案。

更新 2

我创建了一个仅包含 WpfControlLibrary 项目及其依赖项的解决方案。您可以在以下link:

下载

在下面的更新 4

下使用更新的 link

我只是将项目文件夹复制到一个新文件夹中,并将它们添加到新解决方案中。我不得不稍微修改一下 csproj 文件,因为每个文件中的以下部分导致标准 .NET 4 项目引用(System. 等)在解决方案资源管理器中显示为未找到:

<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
  </Target>

我的解决方案是注释掉这部分,这导致项目正确打开。我还删除并重新添加了引用的 Nuget 包,以防万一。

恢复 NuGet 包后,生成解决方案导致以下错误:

Metadata file 'D:\Entegro\VB TVM\Playground18-09-04 VS Build Error\BuildFailure\HunAlmex.Kioszk.Data\bin\Debug\WpfControlLibrary.dll could not be found (Project: WpfControlLibrary; File: CSC)

所以现在重现了上述错误。

我通过从 WpfControlLibrary 项目中排除所有 xaml 文件设法消除了构建错误。我在将它们一一重新添加后构建了解决方案,但无法得出导致错误的结论,因为它各不相同。有一次,通过删除对 HunAlmex.Kioszk.Communication.ImportedServiceContracts 项目的引用,构建错误消失了。

我认为替换 Microsoft.Common.CurrentVersion.targets 文件不是一个好的解决方案,所以我希望有人能提出更好的解决方案。提前致谢。

更新 3

VS 15.8.3 刚出来。更新到它之后,构建示例解决方案的过程发生了一些变化。构建错误如下:

The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://www.postsharp.net/links/nuget-restore.  WpfControlLibrary   D:\Entegro\VB TVM\Playground18-09-04 VS Build Error\BuildFailure\WpfControlLibrary\WpfControlLibrary.csproj

错误在后续构建中仍然存在,并且永远不会消失,除非我只构建 WpfControlLibrary 项目。然后我们回到最初的构建错误。

更新到 VS 15.8.4 也没有解决它。

因为这些项目已经看到自 VS 2010 以来许多版本的 VS 来来去去,我有预感 csproj 文件可能处于不一致的状态。我将尝试创建新项目并向其中添加代码文件。

更新 4

我刚刚用新项目创建了一个新解决方案,并向其中添加了代码文件。不幸的是,构建以同样的错误结束。您可以在这里下载解决方案:

https://drive.google.com/open?id=1oMtwc8aD0kxE6jmtoF05Z5ROZ7diQzUQ

更新 5

这个问题可能与 Microsoft.Common.CurrentVersion.targets 文件无关,因为我将它与相同版本的另一个 VS Enterprise 安装进行了比较,它们匹配。

我会尝试卸载并重新安装 VS。

更新 6

解决方案中的一些项目使用了 PostSharp NuGet 包版本 4.1.23。将它们更新到最新版本 6.0.27,解决了这个问题。该解决方案再次构建良好。

更新 - 事实证明 Visual Studio 更新中有重大变化,大约 15.8.3,这导致使用早期版本的 PostSharp 出现问题 - 我认为 Drew 和我自己在 4.x .将 PostSharp 更新到最新版本 6.0.27 后,在努力处理在我的方面引入的一些重大更改后,问题就解决了。

原文post如下。

这周我几个月没碰过的项目也遇到了同样的问题。我在周二克隆了 repo,NCrunch 构建得很好。昨天,我将 VS2017 更新到 15.8.4,NCrunch 仍在构建它。但是...现在当我按 F5 键时,32 个项目中有 4 个给我一个 CS0006。查看详细的构建日志,我看到了这个:

1>Target "ResolveAssemblyReferences" in file "C:\Program Files (x86)\Microsoft Visual Studio17\Community\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets" from project "D:\Repos\g\Game.Client.Wpf\Game.Client.Wpf_ftfxk2qp_wpftmp.csproj" (target "PostSharp30InspectReferences" depends on it):  
1>Using "ResolveAssemblyReference" task from assembly "Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".  
1>Task "ResolveAssemblyReference"  
1>  TargetFrameworkMoniker:  
1>      .NETFramework,Version=v4.7.2  
1>  TargetFrameworkMonikerDisplayName:  
1>      .NET Framework 4.7.2  
1>  TargetedRuntimeVersion:  
1>      v4.0.30319  
1>  Assemblies:  
1>      System.Core  
1>  AssemblyFiles:  
1>      D:\Repos\g\EntityComponentSystem\bin\Debug\Lafs2.dll  
1>      D:\Repos\g\Framework.Unity\bin\Debug\Lafs2.dll  
1>      D:\Repos\g\Framework\bin\Debug\Lafs2.dll  
1>      D:\Repos\g\Game.Controls.Wpf\bin\Debug\Lafs2.dll  
1>      D:\Repos\g\Game.Messages\bin\Debug\Lafs2.dll  
1>      D:\Repos\g\Logging.Metrics\bin\Debug\Lafs2.dll  
1>      D:\Repos\g\Logging\bin\Debug\Lafs2.dll  
1>      D:\Repos\g\NetCode\bin\Debug\Lafs2.dll  
1>      D:\Repos\g\Universe.Client\bin\Debug\Lafs2.dll  
1>      D:\Repos\g\Universe.Physics\bin\Debug\Lafs2.dll  
1>      D:\Repos\g\Universe\bin\Debug\Lafs2.dll  
1>      C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\mscorlib.dll  

当前构建项目的程序集名称已替换 AssemblyFiles 中所有项目的文件名。 CS0006 说它找不到 D:\Repos\g\Universe\bin\Debug\Lafs2.dll 而它应该寻找 D:\Repos\g\Universe\bin\Debug\Universe.dll.