MSBuild 正在用旧版本替换 Newtonsoft.Json.dll

MSBuild is replacing Newtonsoft.Json.dll with an older version

我正在使用 TeamCity 中的 MSBuild 运行程序来构建 ASP.net 网络 api 和 运行 单元测试。一切正常,直到我升级到 "Microsoft Build Tools 2017 15.7.2"。

在构建解决方案时,msbuild 突然将 Newtonsoft.Json.dll(版本 6.0.4.17603)从 "C:\Program Files (x86)\ISS\Microsoft Web Deploy V3" 或 "C:\Program Files\ISS\Microsoft Web Deploy V3" 复制到输出文件夹。所有项目都使用 NuGet 引用 9.0.1 版本。

在构建 运行 时监控输出文件夹,我可以看到 .dll 在 6.0.4 和 9.0.1 之间来回切换,直到构建结束,而 6.0.4 版本仍然存在。

我发现 this question,当我将 Web 部署文件夹中的 Newtonsoft.Json.dll 文件重命名为 Newtonsoft.Json_old.dll" 时,msbuild 没有替换我的 9.0.1 版本,一切正常.

我已检查所有引用 Newtonsoft.Json 的项目都引用 9.0.1 版本并在 .csproj 文件中使用正确的提示路径。

有人知道如何解决这个问题吗?我的解决方案似乎更像是一种解决方法,我想知道为什么 msbuild 首先要复制此文件。

可能值得触发自定义构建并勾选 'clean all files in the checkout directory before the build' - 您可能有冲突的构建工具挥之不去。

总结

当 MSBuild 解析程序集时,它会在一些非常奇怪的目录中搜索,包括 Web Deploy 文件夹,具体取决于您安装的内容。根据 MSBuild 参考,我认为这是遗留行为。您可以使用项目文件中定义的 MSBuild 属性 来阻止它执行此操作。

在受影响的项目文件中,找到以下行:

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

并在下面添加:

<PropertyGroup>
    <AssemblySearchPaths>$(AssemblySearchPaths.Replace('{AssemblyFolders}', '').Split(';'))</AssemblySearchPaths>
</PropertyGroup>

这将导致 MSBuild 在解析程序集时不再查找有问题的文件夹。


全文

当我们搬到 Visual Studio 2019 时,我的团队 运行 遇到了类似的问题。我们的一些项目仍然以 .NET Framework 4.0 为目标,并且在我们的计算机上安装 Visual Studio 2019 之后构建代理,我们开始在引用我们的一些核心库的项目中遇到神秘错误:

The primary reference "OurCoreLibrary, Version=3.4.2.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx, processorArchitecture=MSIL" could not be resolved because it has an indirect dependency on the assembly "Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed" which was built against the ".NETFramework,Version=v4.5" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.0".

将项目切换到目标 4.5 后问题就消失了,但由于我不会在这里详细说明的原因,我们无法对每个受影响的项目都这样做,所以我决定深入挖掘一下。

事实证明,您的问题提供了一些关于正在发生的事情的见解。我们引用的 Newtonsoft.Json 版本与 "C:\Program Files (x86)\ISS\Microsoft Web Deploy V3" 中的版本匹配,当我删除文件时,构建成功。

我们的具体问题是 Web Deploy 文件夹中 Newtonsoft.Json 的副本是同一版本 (9.0.0.0) 但错误的框架(4.5 而不是 4.0),无论出于何种原因解析逻辑不检查目标框架,导致构建时不匹配。更新到 VS2019 涉及更新 Web Deploy,这也将 Newtonsoft.Json 的副本更新为 9.0.0.0,导致我们发生冲突。

为了了解为什么甚至开始查看该程序集,我将 MSBuild 项目构建输出详细程度设置为 Diagnostic 并查看发生了什么。搜索有问题的路径表明,在 ResolveAssemblyReferences 任务中,MSBuild 正在通过一些意想不到的地方来查找匹配项:

1>          For SearchPath "{AssemblyFolders}". (TaskId:9)
1>          Considered "C:\Program Files (x86)\Microsoft.NET\ADOMD.NET0\OurCoreLibrary.winmd", but it didn't exist. (TaskId:9)
1>          Considered "C:\Program Files (x86)\Microsoft.NET\ADOMD.NET0\OurCoreLibrary.dll", but it didn't exist. (TaskId:9)
1>          Considered "C:\Program Files (x86)\Microsoft.NET\ADOMD.NET0\OurCoreLibrary.exe", but it didn't exist. (TaskId:9)
1>          Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\OurCoreLibrary.winmd", but it didn't exist. (TaskId:9)
1>          Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\OurCoreLibrary.dll", but it didn't exist. (TaskId:9)
1>          Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\OurCoreLibrary.exe", but it didn't exist. (TaskId:9)
1>          Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\OurCoreLibrary.winmd", but it didn't exist. (TaskId:9)
1>          Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\OurCoreLibrary.dll", but it didn't exist. (TaskId:9)
1>          Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\OurCoreLibrary.exe", but it didn't exist. (TaskId:9)
1>          Considered "C:\Program Files\IIS\Microsoft Web Deploy V3\OurCoreLibrary.winmd", but it didn't exist. (TaskId:9)
1>          Considered "C:\Program Files\IIS\Microsoft Web Deploy V3\OurCoreLibrary.dll", but it didn't exist. (TaskId:9)
1>          Considered "C:\Program Files\IIS\Microsoft Web Deploy V3\OurCoreLibrary.exe", but it didn't exist. (TaskId:9)
1>          Considered "C:\Program Files (x86)\Microsoft SQL Server0\SDK\Assemblies\OurCoreLibrary.winmd", but it didn't exist. (TaskId:9)
1>          Considered "C:\Program Files (x86)\Microsoft SQL Server0\SDK\Assemblies\OurCoreLibrary.dll", but it didn't exist. (TaskId:9)
1>          Considered "C:\Program Files (x86)\Microsoft SQL Server0\SDK\Assemblies\OurCoreLibrary.exe", but it didn't exist. (TaskId:9)

进一步挖掘发现,搜索到的路径作为AssemblySearchPaths传入,定义在Microsoft.Common.CurrentVersion.targets:

<AssemblySearchPaths Condition=" '$(AssemblySearchPaths)' == ''">
  {CandidateAssemblyFiles};
  $(ReferencePath);
  {HintPathFromItem};
  {TargetFrameworkDirectory};
  $(AssemblyFoldersConfigFileSearchPath)
  {Registry:$(FrameworkRegistryBase),$(TargetFrameworkVersion),$(AssemblyFoldersSuffix)$(AssemblyFoldersExConditions)};
  {AssemblyFolders};
  {GAC};
  {RawFileName};
  $(OutDir)
</AssemblySearchPaths>

根据MSBuild Task Reference for the ResolveAssemblyReferences taskSearchPaths参数定义为:

Specifies the directories or special locations that are searched to find the files on disk that represent the assemblies. The order in which the search paths are listed is important. For each assembly, the list of paths is searched from left to right. When a file that represents the assembly is found, that search stops and the search for the next assembly starts.

...它定义了一些特殊常量,包括我们的朋友 {AssemblyFolders}:

  • {AssemblyFolders}: Specifies the task will use the Visual Studio.NET 2003 finding-assemblies-from-registry scheme.

因为目录是按顺序检查的,所以您可能希望 {HintPathFromItem} 优先,在大多数情况下确实如此。但是,如果你有一个依赖于旧版本 Newtonsoft.Json 的依赖项,那么该版本 将不会有 HintPath ,因此它将继续,直到它解决.

Later on in Microsoft.Common.CurrentVersion.targets我们可以看到,在某些情况下,这个常量被显式移除,这就是上面答案的来源:

<PropertyGroup Condition="'$(_TargetFrameworkDirectories)' == '' and '$(AssemblySearchPaths)' != '' and '$(RemoveAssemblyFoldersIfNoTargetFramework)' == 'true'">
  <AssemblySearchPaths>$(AssemblySearchPaths.Replace('{AssemblyFolders}', '').Split(';'))</AssemblySearchPaths>
</PropertyGroup>

删除此常量会从考虑中删除有问题的文件夹,老实说,我想不出我希望程序集隐式解析到 Newtonsoft.Json 的任何版本的情况Web Deploy 或 SQL Server SDK 文件夹。话虽这么说,但我确信在某些情况下关闭此功能会导致某些问题,因此请记住这一点。

我找到了另一个简单的解决方案。我手动移动了

<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
      <HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
    </Reference>

到带有引用的最后一个 ItemGroup 部分。