【发布时间】:2015-03-22 22:29:20
【问题描述】:
我正在尝试将旧版 Web 应用程序从 .net 3.5 升级到 .net 4.5,以便能够使用 Microsoft SignalR 库。如果我只是将 .net 版本更改为 4.5,一切正常,但是当我尝试引用 SignalR 时,一切都崩溃了。
也许还有更多问题潜伏在表面之下,但我不断收到有关名为 Newtonsoft.Json 的库的错误消息。
为了说明这一点,Web 应用程序已经引用了旧版本的 Newtonsoft.Json。
令人困惑的是,NuGet 包管理器在尝试安装 Newtonsoft.Json 6.0.8(撰写本文时的最新版本)时抛出错误:
Failed to generate binding redirects for XXXXXXXXXX. An item with the same key has already been added.
我决定,如果该工具无法正常工作而不给我带来麻烦,我只需手动引用该文件即可。 NuGet 确实成功安装了项目目录中的文件,所以我做了以下操作:
我展开了我的 Web 应用程序下的“References”文件夹并删除了对 Newtonsoft.Json 的引用。我点击“添加参考”并浏览到.\packages\Newtonsoft.Json.6.0.8\lib\net45Newtonsoft.Json.dll。
但是,当我尝试运行该应用程序时,我收到以下错误:Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) 这在多个层面上都没有意义:对于初学者来说,Newtonsoft.Json 的旧版本是 3.0。其次,我添加的文件被特别标记为版本 6.0.8,但最奇怪的是,当我在 Visual Studio 2012 中查看引用的属性时,我看到以下内容:
Path: XXXXX\Framework\bin\lib\Newtonsoft.Json.dll
Runtime: Version v2.0.50727
Version: 3.0.0
出于某种原因,Visual Studio 引用了不同目录中的不同文件(以前的 .net 框架的以前版本)!
使事情复杂化的是,“解决方案”中的其他“项目”使用以前版本的 Newtonsoft.Json,但这似乎并不能解释为什么我会点击添加引用 -> 浏览,选择 6.0. 8 版本并获取对不同目录中不同文件的引用。
如果这有什么不同的话,我看不到这个文件在 GAC 中的任何地方。
请帮我解决这个问题。
根据下面@ChrisBint 的回答进行编辑
newtonsoft.json 的 web.config 中没有 bindingRedirect 标签。
在控制台包管理器上输入Add-BindingRedirect 没有任何作用,Visual Studio 仍然引用错误的文件。
另一个编辑:
为了查看问题是否存在于一个项目中,我尝试将另一个 Web 应用程序项目添加到我的解决方案中。我添加了对 Newtonsoft.Json 6.0.8 的引用,看来该项目中一切正常。
另外,我上次执行Add-BindingRedirect 命令时脑残了。我选择了错误的项目。当我选择了正确的项目时,它会说:
PM> Add-BindingRedirect
Add-BindingRedirect : An item with the same key has already been added.
At line:1 char:20
+ Add-BindingRedirect <<<<
+ CategoryInfo : NotSpecified: (:) [Add-BindingRedirect], ArgumentException
+ FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.AddBindingRedirectCommand
另一个编辑: 当我继续调查时,我发现了一些奇怪的东西。如果我在项目中搜索任何引用的名称,我会发现它列在 XXXXX.csproj.properties 中。但是,在那里找不到 Newtonsoft.Json,.properties 文件也不包含目录。 Visual Studio 怎么知道在哪里查找文件?
【问题讨论】:
标签: .net visual-studio visual-studio-2012 reference version