ReportViewer 14:"The primary reference [...] could not be resolved" 同时针对 .NET Framework 4.0

ReportViewer 14: "The primary reference [...] could not be resolved" while targeting .NET Framework 4.0

我已经在我的 Windows Forms 应用程序(针对 .NET Framwork 4.0)中安装了 ReportViewer 14 NuGet 包:Microsoft.ReportingServices.ReportViewerControl.Winforms(V.140.340.80;最新稳定版)

构建时,我收到了一些类似于以下内容的警告:

The primary reference "Microsoft.ReportViewer.Design, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL" could not be resolved because it has an indirect dependency on the assembly "Microsoft.VisualStudio.Shell.14.0, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which was built against the ".NETFramework,Version=v4.5" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.0".

应用程序构建成功,一切似乎都正常,但我仍然有点担心警告。

我不确定它是否与 OS 有关,但是:我是 运行 Windows 7,我的同事在 Windows 10 而不是看到这个问题(都是 Visual Studio 2017)

不幸的是,我们目前绑定到 .NET Framework 4.0,因为我们必须支持一些 Windows XP 客户端。

更新:

我创建了一个新的空 .NET 4.0 项目并收到相同但更少的警告(这次只影响“[.NET] 框架程序集”)。

以下是原版项目的完整警告列表:

1>C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets(1987,5): warning MSB3268: The primary reference "Microsoft.ReportViewer.Design" could not be resolved because it has an indirect dependency on the framework assembly "System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "Microsoft.ReportViewer.Design" or retarget your application to a framework version which contains "System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
1>C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets(1987,5): warning MSB3258: The primary reference "Microsoft.ReportViewer.Design" could not be resolved because it has an indirect dependency on the .NET Framework assembly "Microsoft.Build.Framework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which has a higher version "14.0.0.0" than the version "4.0.0.0" in the current target framework.
1>C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets(1987,5): warning MSB3268: The primary reference "Microsoft.ReportViewer.Design" could not be resolved because it has an indirect dependency on the framework assembly "System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "Microsoft.ReportViewer.Design" or retarget your application to a framework version which contains "System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
1>C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets(1987,5): warning MSB3268: The primary reference "Microsoft.ReportViewer.Design" could not be resolved because it has an indirect dependency on the framework assembly "System.Threading.Tasks, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "Microsoft.ReportViewer.Design" or retarget your application to a framework version which contains "System.Threading.Tasks, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
1>C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets(1987,5): warning MSB3268: The primary reference "Microsoft.ReportViewer.Design" could not be resolved because it has an indirect dependency on the framework assembly "System.Diagnostics.Tracing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "Microsoft.ReportViewer.Design" or retarget your application to a framework version which contains "System.Diagnostics.Tracing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
1>C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets(1987,5): warning MSB3268: The primary reference "Microsoft.ReportViewer.Design" could not be resolved because it has an indirect dependency on the framework assembly "System.Collections.Concurrent, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "Microsoft.ReportViewer.Design" or retarget your application to a framework version which contains "System.Collections.Concurrent, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
1>C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets(1987,5): warning MSB3268: The primary reference "Microsoft.ReportViewer.Design" could not be resolved because it has an indirect dependency on the framework assembly "System.Dynamic.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "Microsoft.ReportViewer.Design" or retarget your application to a framework version which contains "System.Dynamic.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
1>C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets(1987,5): warning MSB3268: The primary reference "Microsoft.ReportViewer.Design" could not be resolved because it has an indirect dependency on the framework assembly "System.Collections, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "Microsoft.ReportViewer.Design" or retarget your application to a framework version which contains "System.Collections, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
1>C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets(1987,5): warning MSB3268: The primary reference "Microsoft.ReportViewer.Design" could not be resolved because it has an indirect dependency on the framework assembly "System.Threading, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "Microsoft.ReportViewer.Design" or retarget your application to a framework version which contains "System.Threading, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
1>C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets(1987,5): warning MSB3268: The primary reference "Microsoft.ReportViewer.Design" could not be resolved because it has an indirect dependency on the framework assembly "System.Resources.ResourceManager, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "Microsoft.ReportViewer.Design" or retarget your application to a framework version which contains "System.Resources.ResourceManager, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
1>C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets(1987,5): warning MSB3268: The primary reference "Microsoft.ReportViewer.Design" could not be resolved because it has an indirect dependency on the framework assembly "System.Globalization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "Microsoft.ReportViewer.Design" or retarget your application to a framework version which contains "System.Globalization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
1>C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets(1987,5): warning MSB3268: The primary reference "Microsoft.ReportViewer.Design" could not be resolved because it has an indirect dependency on the framework assembly "System.Diagnostics.Debug, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "Microsoft.ReportViewer.Design" or retarget your application to a framework version which contains "System.Diagnostics.Debug, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
1>C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets(1987,5): warning MSB3268: The primary reference "Microsoft.ReportViewer.Design" could not be resolved because it has an indirect dependency on the framework assembly "System.Runtime.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "Microsoft.ReportViewer.Design" or retarget your application to a framework version which contains "System.Runtime.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
1>C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets(1987,5): warning MSB3268: The primary reference "Microsoft.ReportViewer.Design" could not be resolved because it has an indirect dependency on the framework assembly "System.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "Microsoft.ReportViewer.Design" or retarget your application to a framework version which contains "System.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
1>C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets(1987,5): warning MSB3268: The primary reference "Microsoft.ReportViewer.Design" could not be resolved because it has an indirect dependency on the framework assembly "System.Diagnostics.Tools, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "Microsoft.ReportViewer.Design" or retarget your application to a framework version which contains "System.Diagnostics.Tools, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
1>C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets(1987,5): warning MSB3268: The primary reference "Microsoft.ReportViewer.Design" could not be resolved because it has an indirect dependency on the framework assembly "System.Reflection, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "Microsoft.ReportViewer.Design" or retarget your application to a framework version which contains "System.Reflection, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".

终于解决了:

TL;DR

核心问题好像是分别安装了Visual Studio2015SQLServer Management Studio 17.2 : 卸载所有组件后,警告消失了!

长答案

我注意到的第一件事是,MSBuild 抱怨的程序集(例如 Microsoft.VisualStudio.Shell.14.0)安装在我的全局程序集缓存 (GAC) 中,而它们不在我同事的 PC 上。我通过 运行 在开发人员命令提示符中验证了以下内容:

gacutil /l Microsoft.VisualStudio.Shell.14.0

所以一定是某些东西在 GAC 中安装了这些程序集。

版本号“14”指向Visual Studio2015方向(因为其内部版本号为Visual Studiov14)。所以我卸载了名称中包含 "Visual Studio 2015" 的所有内容 - 包括

  • Visual Studio 2015 Shell(隔离)和
  • SQL Server Management Studio 17.2(因为我知道 SSMS 基本上是一个 Visual Studio 扩展,并且正在安装 "Shell"-VS 版本)

这解决了所有警告。

(我假设 MSBuild 然后正在使用适用于 .NET 4.0 的参考程序集,而不是来自 GAC 的针对 4.5 的参考程序集)

为了仔细检查我的发现,我重新安装了 SQL Server Management Studio 17.2,其中包括 VS2015 Shell(已隔离) - 并且返回了警告。 又卸载了一遍又没了

虽然这不是一个完全令人满意的解决方案,因为我无法再安装 SSMS。

我已通过删除对 "Microsoft.ReportViewer.Design"

的所有引用来修复此问题

(此引用由报表查看器 NuGet 包添加。在 VS2010/VS2015 我从未在我的应用程序中使用过该引用或分发过该 dll。)

所以我的解决方案如下:

  • VS 2017,SSMS 17.x 均已安装
  • 针对 .net 4.0 的 Winform 应用程序
  • 添加了报表查看器块包和 "Microsoft rdlc reports designer for visual studio" 扩展
  • 手动删除 Microsoft.RepotViewer.Design 引用
  • 我的应用程序只包含本地报表:旧格式(vs2010)的rdlc文件,以及一些修改后的文件,由VS报表设计器转换为最新的rdlc。
  • 应用程序编译正常,没有警告,并且在开发机器上运行没有任何问题。
  • 我的 WIX 安装程序仅安装这些与报告相关的组件:Microsoft.ReportViewer.Common.dll、Microsoft.ReportViewer.DataVisualization.dll、Microsoft.ReportViewer.ProcessingObjectModel.dll、Microsoft.ReportViewer.WinForms.dll、Microsoft.SqlServer.Types.dll

在 win10 上测试没有任何问题。

在带有 .net 4.0 的干净 XP SP3 上进行压力测试:报告 运行 正常。