使用多个项目时Newtonsoft.Json.dll问题 [英] Newtonsoft.Json.dll issue when using multiple projects

查看:3669
本文介绍了使用多个项目时Newtonsoft.Json.dll问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了一些非常奇怪的行为,我一直无法纠正相关的Newtonsoft.Json.dll引用。我有一个样品溶液设置与以下项目:


  • JsonProblem.Core

  • JsonProblem.CauseProblem(引用JsonProblem.Core)

  • JsonProblem.Web(引用JsonProblem.Core和JsonProblem.CauseProblem)

在JsonProblem.Core和JsonProblem.Web我已经加入了微软的ASP.NET Web API 2.2的NuGet包。在JsonProblem.Core我创建了一个网页API。如果我建立JsonProblem.Core并运行JsonProblem.Web一切页按预期工作。

现在,如果我建立JsonProblem.CauseProblem并尝试JsonProblem.Web查看的网页,我碰到下面的错误。


  

无法加载文件或程序集Newtonsoft.Json或之一其
  依赖。找到的程序集清单定义不
  匹配程​​序集引用。 (异常来自HRESULT:0x80131040)


如果我重建JsonProblem.Core错误消失。并再次,如果我建JsonProblem.CauseProblem没有建立JsonProblem.Core后(即使JsonProblem.CauseProblem取决于JsonProblem.Core)我得到的错误。不知怎的,JsonProblem.CauseProblem的积聚是造成Newtonsoft.Json版本4.5.11得到复制到JsonProblem.Web bin目录,覆盖6.0.3版。我是pretty知道我有约束力的正确重定向设置,如我在web.config中JsonProblem.Web以下并在JsonProblem.Core和JsonProblem.CauseProblem在app.config文件:

 < dependentAssembly>
< assemblyIdentity名称=Newtonsoft.Json公钥=30ad4fe6b2a6aeed文化=中性/>
< bindingRedirect oldVersion =0.0.0.0-6.0.0.0NEWVERSION =6.0.0.0/>
< / dependentAssembly>

所以,我很茫然,以这种奇怪的行为的原因。我在2个项目复制它。看来,绑定重定向时,我建立JsonProblem.CauseProblem忽略。我可以解决它,但我担心什么错误或导致此行为可能会改变的背景下其他引用可能导致上下行问题。


编辑 - 作为如痴如醉建议我用fuslogvw工具。这里是什么在日志中产生的。我不知道如何跨preT这一点,但是,由于日志不告诉我在编译的时候会发生什么覆盖Newtonsoft.Json.dll的版本在我的应用程序的网站目录。

 操作失败。
绑定结果:HR = 0x80131040。没有可用的描述。C:从加载的程序集经理\\ WINDOWS \\ Microsoft.NET \\ Framework64 \\ v4.0.30319 \\ clr.dll
根据运行可执行C:\\ Program Files文件\\ IIS防爆preSS \\ iisex press.exe
---详细的错误日志如下。=== pre-绑定状态信息===
日志:显示名称= Newtonsoft.Json
 (部分)
警告:部分绑定信息是为组装供应:
警告:程序集名称:Newtonsoft.Json |域ID:5
警告:如果仅提供集显示名称的一部分发生了一个部分绑定。
警告:这可能导致粘合剂加载了不正确的装配。
警告:建议为大会提供了一个完全指定的文字标识,
警告:一个由简单的名称,版本,区域性和公钥令牌。
警告:请参阅白皮书http://go.microsoft.com/fwlink/?LinkId=109270以获取更多信息和应对这一问题的共同解决方案。
日志:应用平台=文件:/// C:/Users/John/Desktop/JsonProblem/JsonProblem.Web/
LOG:初始PrivatePath = C:\\用户\\约翰\\桌面\\ JsonProblem \\ JsonProblem.Web \\ BIN
日志:动态基= C:\\用户\\约翰\\应用程序数据\\本地\\ TEMP \\临时ASP.NET文件\\ VS \\ 3661babd
日志:缓存基= C:\\用户\\约翰\\应用程序数据\\本地\\ TEMP \\临时ASP.NET文件\\ VS \\ 3661babd
LOG:AppName的= 3b3fd45
调用汇编:(未知)。
===
日志:此绑定的默认加载上下文开始。
日志:正在使用应用程序配置文件:C:\\用户\\约翰\\桌面\\ JsonProblem \\ JsonProblem.Web \\ web.config中
日志:使用主机配置文件:C:\\用户\\约翰\\文档\\ IISEx preSS \\设置\\ aspnet.config
日志:从C使用计算机配置文件:\\ WINDOWS \\ Microsoft.NET \\ Framework64 \\ v4.0.30319 \\ CONFIG \\ machine.config中。
日志:没有被应用政策在这个时候(私人,自定义,局部的,或基于位置的程序集绑定)来引用。
日志:新的URL文件试图下载:/// C:/用户/约翰/应用程序数据/本地/温度/ ASP.NET临时文件/ VS / 3661babd / 3b3fd45 / Newtonsoft.Json.DLL。
日志:新的URL文件试图下载:/// C:/用户/约翰/应用程序数据/本地/温度/ ASP.NET临时文件/ VS / 3661babd / 3b3fd45 / Newtonsoft.Json / Newtonsoft.Json.DLL。
日志:新的URL文件试图下载:/// C:/Users/John/Desktop/JsonProblem/JsonProblem.Web/bin/Newtonsoft.Json.DLL。
日志:程序集下载成功。文件试图设置:C:\\用户\\约翰\\桌面\\ JsonProblem \\ JsonProblem.Web \\ BIN \\ Newtonsoft.Json.dll
日志:进入下载缓存安装阶段。
日志:大会名称为:Newtonsoft.Json,版本= 4.5.0.0,文化=中性公钥= 30ad4fe6b2a6aeed
日志:一个部分指定的程序集绑定从应用程序目录成功。需要重新申请策略。
日志:正在使用应用程序配置文件:C:\\用户\\约翰\\桌面\\ JsonProblem \\ JsonProblem.Web \\ web.config中
日志:使用主机配置文件:C:\\用户\\约翰\\文档\\ IISEx preSS \\设置\\ aspnet.config
日志:从C使用计算机配置文件:\\ WINDOWS \\ Microsoft.NET \\ Framework64 \\ v4.0.30319 \\ CONFIG \\ machine.config中。
日志:在重定向应用程序配置文件中发现:4.5.0.0重新定向到6.0.0.0。
日志:后政策参考:Newtonsoft.Json,版本6.0.0.0 =文化=中性公钥= 30ad4fe6b2a6aeed
日志:GAC查找不成功。
日志:政策组装后的参考需要再次探测。
日志:新的URL文件试图下载:/// C:/用户/约翰/应用程序数据/本地/温度/ ASP.NET临时文件/ VS / 3661babd / 3b3fd45 / Newtonsoft.Json.DLL。
日志:新的URL文件试图下载:/// C:/用户/约翰/应用程序数据/本地/温度/ ASP.NET临时文件/ VS / 3661babd / 3b3fd45 / Newtonsoft.Json / Newtonsoft.Json.DLL。
日志:新的URL文件试图下载:/// C:/Users/John/Desktop/JsonProblem/JsonProblem.Web/bin/Newtonsoft.Json.DLL。
日志:程序集下载成功。文件试图设置:C:\\用户\\约翰\\桌面\\ JsonProblem \\ JsonProblem.Web \\ BIN \\ Newtonsoft.Json.dll
日志:进入下载缓存安装阶段。
日志:大会名称为:Newtonsoft.Json,版本= 4.5.0.0,文化=中性公钥= 30ad4fe6b2a6aeed
警告:比较程序集名称时发生不匹配:主版本
错误:程序集引用不匹配发现大会的定义。
错误:安装程序失败,HR = 0x80131040。
错误:未能完成组装(HR = 0x80131040)的设置。探测终止。


解决方案

我有相同的行为与上述问题说明,除了当我重建类库(即JsonProblem.Core),它会拉Newtonsoft。 dll的4.5.11,尽管这个类库有这样的指示给NewtonSoft参考不直接引用或包的NuGet。

在什么可以被认为是一个Visual Studio 2015年的错误,类库重建复制无形引用的DLL引用到项目箱文件夹(而不是目前正在建设),在参考项目覆盖的DLL,打破他们。

IOW,例如,建立项目Y(引用JsonProblem.Core),明显的成功。建设项目X(裁判JsonProblem.Core)打破项目Y.生成项目Y和现在这个项目突破十无限循环:而(理智){建(); }

在我的情况下,我在此引用库的唯一第三方库是Gibraltar.Agent和Gibraltar.Agent.Web.Mvc(其中报告了NewtonSoft没有依赖性。这些点击鼠标右键在项目引用,选择查找code这个模块的依赖透露,我并不需要引用第二之一。

我卸载Gibraltar.Agent.Web.Mvc中的NuGet,所有的问题就走了。 DLL地狱心痛两天已经结束。

I am seeing some really odd behavior that I have not been able to correct related to references for Newtonsoft.Json.dll. I have a sample solution set up with the following projects:

  • JsonProblem.Core
  • JsonProblem.CauseProblem (references JsonProblem.Core)
  • JsonProblem.Web (references JsonProblem.Core and JsonProblem.CauseProblem)

In JsonProblem.Core and JsonProblem.Web I have added the "Microsoft ASP.NET Web API 2.2" NuGet package. In JsonProblem.Core I have created a web api. If I build JsonProblem.Core and run a page from JsonProblem.Web everything works as expected.

Now if I build JsonProblem.CauseProblem and try to view a page in JsonProblem.Web, I get the following error.

Could not load file or assembly 'Newtonsoft.Json' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

If I rebuild JsonProblem.Core the error goes away. And again if I build JsonProblem.CauseProblem without building JsonProblem.Core afterward (even though JsonProblem.CauseProblem depends on JsonProblem.Core) I get the error. Somehow the build of JsonProblem.CauseProblem is causing version 4.5.11 of Newtonsoft.Json to get copied to the JsonProblem.Web bin directory, overwriting version 6.0.3. I'm pretty sure I have binding redirects setup correctly, as I have the following in the JsonProblem.Web web.config and in the app.config files for JsonProblem.Core and JsonProblem.CauseProblem:

<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>

So I am at a loss as to the cause of this strange behavior. I have reproduced it in 2 projects. It seems that the binding redirects are ignored when I build JsonProblem.CauseProblem. I can work around it, but I'm concerned that whatever bug or feature is causing this behavior may be altering other references in the background that might cause problems down the line.


EDIT - as tizzy suggested I used the fuslogvw tool. Here is what was generated in the log. I'm not sure how to interpret this, however, because the log does not tell me what happens at build time to overwrite the version of Newtonsoft.Json.dll in my the app's website directory.

The operation failed.
Bind result: hr = 0x80131040. No description available.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable  C:\Program Files\IIS Express\iisexpress.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = Newtonsoft.Json
 (Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: Newtonsoft.Json | Domain ID: 5
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/Users/John/Desktop/JsonProblem/JsonProblem.Web/
LOG: Initial PrivatePath = C:\Users\John\Desktop\JsonProblem\JsonProblem.Web\bin
LOG: Dynamic Base = C:\Users\John\AppData\Local\Temp\Temporary ASP.NET Files\vs\3661babd
LOG: Cache Base = C:\Users\John\AppData\Local\Temp\Temporary ASP.NET Files\vs\3661babd
LOG: AppName = 3b3fd45
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Users\John\Desktop\JsonProblem\JsonProblem.Web\web.config
LOG: Using host configuration file: C:\Users\John\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Users/John/AppData/Local/Temp/Temporary ASP.NET Files/vs/3661babd/3b3fd45/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Users/John/AppData/Local/Temp/Temporary ASP.NET Files/vs/3661babd/3b3fd45/Newtonsoft.Json/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Users/John/Desktop/JsonProblem/JsonProblem.Web/bin/Newtonsoft.Json.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Users\John\Desktop\JsonProblem\JsonProblem.Web\bin\Newtonsoft.Json.dll
LOG: Entering download cache setup phase.
LOG: Assembly Name is: Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: A partially-specified assembly bind succeeded from the application directory. Need to re-apply policy.
LOG: Using application configuration file: C:\Users\John\Desktop\JsonProblem\JsonProblem.Web\web.config
LOG: Using host configuration file: C:\Users\John\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Redirect found in application configuration file: 4.5.0.0 redirected to 6.0.0.0.
LOG: Post-policy reference: Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: GAC Lookup was unsuccessful.
LOG: The post-policy assembly reference requires probing again.
LOG: Attempting download of new URL file:///C:/Users/John/AppData/Local/Temp/Temporary ASP.NET Files/vs/3661babd/3b3fd45/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Users/John/AppData/Local/Temp/Temporary ASP.NET Files/vs/3661babd/3b3fd45/Newtonsoft.Json/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Users/John/Desktop/JsonProblem/JsonProblem.Web/bin/Newtonsoft.Json.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Users\John\Desktop\JsonProblem\JsonProblem.Web\bin\Newtonsoft.Json.dll
LOG: Entering download cache setup phase.
LOG: Assembly Name is: Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: The assembly reference did not match the assembly definition found.
ERR: Setup failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

解决方案

I had the exact same behavior as the above problem description, except that when I rebuilt my class library (i.e. JsonProblem.Core), it would pull in Newtonsoft.dll 4.5.11, even though this class library had no direct reference or Nuget package that indicated a reference to NewtonSoft.

In what could be considered a Visual Studio 2015 bug, the class library rebuild copies the invisibly referenced DLLs into referencing project bin folders (not currently being built), overwriting the DLLs in referencing projects, breaking them.

IOW, for example, build project Y (referencing JsonProblem.Core), apparent success. Build project X (ref to JsonProblem.Core) which breaks project Y. Build Project Y and now this breaks Project X. Infinite loop: while( sane ) { build(); }

In my case, the only 3rd party libraries that I was referencing in this library were Gibraltar.Agent and Gibraltar.Agent.Web.Mvc (which reported no dependency on NewtonSoft. Right clicking on these in project References and selecting "Find Code Dependent on this module" revealed that I didn't need to reference the 2nd one.

I uninstalled Gibraltar.Agent.Web.Mvc in Nuget, and all problems went away. Two days of DLL Hell heartache have ended.

这篇关于使用多个项目时Newtonsoft.Json.dll问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆