【发布时间】:2014-07-23 23:19:54
【问题描述】:
为了在 Mono 中部署我的项目,我已将其降级为 .Net 4.0,就像我对我引用的库 (CommonUtils) 所做的那样。但是,我仍然收到以下异常:
无法解析主要引用“CommonUtils”,因为它间接依赖于针对“.NETFramework,版本=v4.5" 框架。这是比当前目标框架“.NETFramework,Version=v4.0”更高的版本。
我尝试解决的问题:
- 我已经在两个项目中重新安装了所有 Nuget 包
- 我多次尝试清理项目/解决方案,删除所有 bin/obj 文件夹并重建所有内容
- 我已更改程序集的名称以确保没有缓存任何内容
这些都没有帮助。
这两个项目都设置为编译为 .Net 4.0。参考是通过将编译的程序集 CommonUtils 复制到“ExternalLibraries”文件夹中,因为此 DLL 也用于其他项目。
右键单击 Newtonsoft.Json.dll - 与 CommonUtils 一起移动到 bin 文件夹 - 显示在详细信息/文件描述中:Json.NET .NET 4.0
我很确定我对于 .net 4.0 的一切都是正确的,Visual Studio 还允许我添加库并支持我自动完成,只是编译会引发上述警告。作为警告的结果,我突然得到异常,即从 CommonUtils 引用的类型都不存在
找不到命名空间名称“CommonUtils”的类型(您是否缺少 using 指令或程序集引用?)
我还能尝试什么来完成这项工作?
PS:几年前我遇到过类似的问题,使用旧程序集进行编译,并通过从头开始设置 Windows 和 Visual Studio 来“解决”它,但我只是不想这样做时间。
更新:
它可以在另一台计算机上运行:我已经通过 USB 记忆棒将文件夹结构带到另一台具有相同操作系统(Windows 8.1)和相同 Visual Studio 2013 的计算机上——据我所知,两台计算机都应该有已安装更新 - 并且可以正常工作。
将带有成功编译项目的 USB 记忆棒带回我的电脑 - 我仍然遇到同样的错误。
(我明白,这意味着两台计算机不可能都安装相同的东西,但我不知道如何找到不同之处,也许有人暗示这种方式可能有什么问题?)
更新 2:
进程资源管理器并没有太大帮助,但是当我用诊断日志编译所有内容时,我想我得到了关键提示:
...
5> Unified primary reference "Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed". (TaskId:158)
5> Using this version instead of original version "4.5.0.0" in "C:\<SolutionFolder>\packages\WebGrease.1.6.0\lib\WebGrease.dll" because of a binding redirect entry in the file "Web.config". (TaskId:158)
5> Using this version instead of original version "4.5.0.0" in "C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies\System.Web.Http.dll" because of a binding redirect entry in the file "Web.config". (TaskId:158)
5> Using this version instead of original version "4.5.0.0" in "C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies\System.Net.Http.Formatting.dll" because of a binding redirect entry in the file "Web.config". (TaskId:158)
5> Resolved file path is "C:\<SolutionFolder>\packages\Newtonsoft.Json.6.0.3\lib\net40\Newtonsoft.Json.dll". (TaskId:158)
5> Reference found at search path location "{HintPathFromItem}". (TaskId:158)
5> Found related file "C:\<SolutionFolder>\packages\Newtonsoft.Json.6.0.3\lib\net40\Newtonsoft.Json.xml". (TaskId:158)
5> The ImageRuntimeVersion for this reference is "v4.0.30319". (TaskId:158)
...
但是,我在构建工作的另一台计算机上也收到完全相同的消息。
另外两台电脑都在开头显示这一行:
Skipping NuGet package Newtonsoft.Json 6.0.3 since it is already installed.
installed 在这种情况下是什么意思?那NuGet下载了吗?还是在 GAC 的某个地方,但我找不到? (见 cmets)
在编译工作的机器上,我还得到了这个输出:
1> Dependency "Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed". (TaskId:17)
1> Resolved file path is "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Blend\Newtonsoft.Json.dll". (TaskId:17)
1> Reference found at search path location "{Registry:Software\Microsoft\.NETFramework,v4.0,AssemblyFoldersEx}". (TaskId:17)
1> For SearchPath "C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies". (TaskId:17)
1> Considered "C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies\Newtonsoft.Json.winmd", but it didn't exist. (TaskId:17)
1> Considered "C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies\Newtonsoft.Json.dll", but it didn't exist. (TaskId:17)
1> Considered "C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies\Newtonsoft.Json.exe", but it didn't exist. (TaskId:17)
1> For SearchPath "{TargetFrameworkDirectory}". (TaskId:17)
1> Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Newtonsoft.Json.winmd", but it didn't exist. (TaskId:17)
1> Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Newtonsoft.Json.dll", but it didn't exist. (TaskId:17)
1> Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Newtonsoft.Json.exe", but it didn't exist. (TaskId:17)
1> For SearchPath "{Registry:Software\Microsoft\.NETFramework,v4.0,AssemblyFoldersEx}". (TaskId:17)
1> Considered AssemblyFoldersEx locations. (TaskId:17)
1> Required by "CommonUtils". (TaskId:17)
1> The ImageRuntimeVersion for this reference is "v4.0.30319". (TaskId:17)
但我在我的电脑上看不到它,编译失败
最终更新:
我放弃并重新安装了我的整个系统——它现在可以工作了,但是它没有回答错误的 Newtonsoft.Json.dll 来自哪里以及为什么它没有出现在 GAC 中的问题,我终于可以再次从事那个项目。
我真的很失望,它不像在 maven 中那么简单,你可以删除一个文件夹来让 maven 重新下载所有的库。
我在 VHD 上有损坏系统的映像,所以如果其他人想出了一个很好的解决方案,我可以在 VM 中尝试一下。
【问题讨论】:
-
尝试在解决方案中的所有文件中搜索字符串“4.5.0.0”。
-
另外,检查
CommonUtils的“使用特定版本”标志。也许它设置为“是”并迫使您使用比您想要的更高的版本。 -
你需要关注 indirect 依赖。 CommonUtils 引用了另一个程序集,而那个 程序集是使用新版本的 Newtonsoft.Json.dll 构建的。这在 VS 中是不直接可见的,您可以通过在直接依赖项上运行 ildasm.exe 来查看它。
-
检查是否有人有绝妙的想法将 Newtonsoft.Json 安装到 GAC...
-
尝试使用 ProcessExplorer 之类的工具来查看您的程序从何处加载其 DLL。
标签: c# .net visual-studio-2013