搜索后我尝试过的事情:
在 Web.Config 中对旧版本进行绑定:
它在构建时成功,但在访问站点时出现错误。
编辑
好的,所以我尝试像每个 nuget 包一样重新安装,它似乎已经添加了 newtonsoft.json.dll 的 4.5 版本,但我得到了同样的错误。我的项目的目标框架是 .NET 4.5.1,这是我现在得到的堆栈跟踪:
“/”应用程序中的服务器错误。无法加载文件或程序集 Newtonsoft.Json 或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。 (来自 HRESULT 的异常:0x80131040) 说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。异常详细信息:System.IO.FileLoadException:无法加载文件或程序集 Newtonsoft.Json 或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。 (来自 HRESULT 的异常:0x80131040)源错误:在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。程序集加载跟踪:以下信息有助于确定无法加载程序集 Newtonsoft.Json 的原因。
=== 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: 2
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/user/documents/visual studio 2013/Projects/foo/bar/
LOG: Initial PrivatePath = c:\users\user\documents\visual studio 2013\Projects\foo\bar\bin
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: c:\users\user\documents\visual studio 2013\Projects\foo\bar\web.config
LOG: Using host configuration file: C:\Users\user\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\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/user/AppData/Local/Temp/1/Temporary ASP.NET Files/root/48686d37/9d7a6572/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Users/user/AppData/Local/Temp/1/Temporary ASP.NET Files/root/48686d37/9d7a6572/Newtonsoft.Json/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///c:/users/user/documents/visual studio 2013/Projects/foo/bar/bin/Newtonsoft.Json.DLL.
LOG: Using application configuration file: c:\users\user\documents\visual studio 2013\Projects\foo\bar\web.config
LOG: Using host configuration file: C:\Users\user\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Redirect found in application configuration file: 4.5.0.0 redirected to 6.0.1.0.
LOG: Post-policy reference: Newtonsoft.Json, Version=6.0.1.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: Attempting download of new URL file:///C:/Users/user/AppData/Local/Temp/1/Temporary ASP.NET Files/root/48686d37/9d7a6572/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Users/user/AppData/Local/Temp/1/Temporary ASP.NET Files/root/48686d37/9d7a6572/Newtonsoft.Json/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///c:/users/user/documents/visual studio 2013/Projects/foo/bar/bin/Newtonsoft.Json.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
堆栈跟踪:
[FileLoadException: 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)]
[FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=6.0.1.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)]
System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0
System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +34
System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +152
System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) +77
System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +16
System.Reflection.Assembly.Load(String assemblyString) +28
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +38
[ConfigurationErrorsException: Could not load file or assembly 'Newtonsoft.Json, Version=6.0.1.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)]
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +752
System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +218
System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +130
System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +170
System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies() +91
System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +285
System.Web.Compilation.BuildManager.ExecutePreAppStart() +153
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +516
[HttpException (0x80004005): Could not load file or assembly 'Newtonsoft.Json, Version=6.0.1.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)]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9913572
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18408
为了解决这个问题,我通过运行以下命令并检查结果来确保我的所有项目都使用相同的版本:
update-package Newtonsoft.Json -reinstall
最后,我从 web.config 中删除了以下内容:
<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>
如果要确保所有 Newtonsoft.Json 包的版本相同,可以像这样指定版本:
update-package Newtonsoft.Json -version 6.0.0 -reinstall
注意:要在 Visual Studio 中打开 PMC,请单击工具 -> Nuget 包管理器 -> 包管理器控制台。谢谢@罗斯!
我正在使用 Newtonsoft.Json v6.0.3,但这是我必须在我的 Web.config 文件中执行的操作:
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
请注意,即使我使用的是 6.0.3,我也必须输入 newVersion="6.0.0.0"
在我的 packages.config 文件中,我有:
<package id="Newtonsoft.Json" version="6.0.3" targetFramework="net45" />
在确保我的计算机在所有位置都具有相同的版本并且我的项目都指向相同的参考路径之后,我尝试了以下操作。我还确保旧版本的绑定是他们的并且绑定到我拥有的当前版本的 dll。
我在具有严格框架的环境中工作,并且框架团队经常用不同的 dll 打乱版本控制。
我解决此问题的方法是在 Visual Studio (2013) 中运行包管理器控制台。从那里我运行了以下命令:
update-package Newtonsoft.Json -reinstall
其次是
update-package Newtonsoft.Json
这经历并更新了我所有的配置文件和相关的项目文件。强制它们使用相同版本的 dll。最初是 4.5 版,然后再次更新以获取最新版本。
我有同样的错误信息,就像你提到的,这是由于引用了不同版本的 Newtonsoft.Json.dll。
我的 MVC 解决方案中的一些项目使用了该 dll 版本 4 的 NuGet 包。
然后,我添加了一个 NuGet 包(在我的例子中是 Salesforce),它带来了 Newtonsoft.Json 版本 6,作为其中一个项目的依赖项。这就是引发我问题的原因。
为了清理,我使用解决方案的 NuGet 包管理器中的更新部分(关闭工具菜单或解决方案右键单击)来更新整个解决方案中的 Json.Net 包,因此它对于所有项目都是相同的版本。
之后,我检查了 App Config 文件以确保任何绑定重定向行都转到我选择的版本,如下所示。
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
好的,我想我现在可以开始工作了。我删除了我机器上所有不是我能找到的最新版本的 Newtonsoft.Json.dll,确保我在 NuGet 中拥有最新版本,并构建它并确保它是 bin 文件夹中的最新版本,并且我将更改保留在 web.config 和 .csproj 中。现在我遇到了另一个错误,所以它必须工作..
通过在我的 web.config 上将这部分注释掉,解决了我的问题:
<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>
但是当然,您需要通过在包管理器控制台中执行此操作来确保您已更新或拥有正确的版本:
update-package Newtonsoft.Json -reinstall
经过几个小时的努力……我做了以下事情:
网络配置
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
</dependentAssembly>
<!-- other assemblies... -->
</assemblyBinding>
</runtime>
输入正确的新版本和旧版本号的关键是打开项目的 References
找到包转到其属性或点击 alt + enter
在使用 Manage NuGet 进行探索时,您会发现一个部分 Version
,在我的例子中是 12.0.0.0
,而实际上是 12.0.3
。
所以你需要从参考属性中选择包的版本(在我的例子中是 12.0.0.0)
最后,clean
和 rebuild
项目(您可能想要删除之前的 bin
和 obj
文件夹)。
你可能会遇到其他包依赖问题,我为所有人做了这件事,他们工作了。
您可以通过在 web.config 文件中添加以下行来解决此问题。
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
就我而言,这只是一个问题:
Tools -> NuGet Package Manager -> Package Manager Settings -> Clear Cache
问题是我重新映射 TFS 文件夹时引起的。
我已经尝试了 Oleg 的步骤,它们适用于我同样的情况。
脚步:
在包管理器中运行 update-package Newtonsoft.Json -reinstall。通过启用查看隐藏文件并删除 bin 文件夹来删除您的 bin。关闭您的 Visual Studio 并重新打开它。现在再次运行您的项目。我相信应该没问题!
真正帮助我的是关闭 Resharper 构建并在我的项目中使用 VisualStudio Re-Build 选项。
从工具>>NuGet 包管理器>>管理包以将所有解决方案的 Newtonsoft.Json 更新到最新版本
如果您正在使用某种颠覆:删除项目并重新下载它,它对我有用:S
我在引用 MVC 5 项目的测试项目上遇到了同样的问题。这发生在我与 MVC 5 项目合并之后。我通过安装 Visual Studio 2012 更新来修复它,正如 Andre 的回复中解释的 here。
在您的 Visual Studio 中,转到工具 > 扩展和更新 在左侧的手风琴列表中选择“更新”。选择“产品更新”。更新到最新版本
工具> 扩展和更新
在左侧的手风琴列表中选择“更新”。
选择“产品更新”。
更新到最新版本
我不知道为什么,但在我的情况下,即使我从项目中删除 bin 文件夹,当我构建项目时它会复制旧版本的 newtonsoft.json,我从包文件夹中复制了新版本的 dll,它现在可以解决。
如果这些都不起作用,请检查以下答案:
https://stackoverflow.com/a/12011221/150370
(TL;DR:从配置文件中的 configuration
标记中删除 xml 命名空间。否则绑定重定向将被忽略)
在某些情况下,如果您删除 Bin
和 Obj
文件夹将解决此问题。
好吧,所以我想要做的是将我的答案添加到这个长长的列表中,而不是创建一个重复的问题......
如果您在 2019 年使用 .NET Core 3.0(此时为预览版),则解决方案是确保所有项目都针对相同的 .NET Core 版本(在我的情况下为 3.0)。我想我有一个针对 2.1 的解决方案项目,其余的都是 2.2,所以我可能会坚持使用 2.2...
我什至没有在任何项目中安装 Newtonsoft.Json,自然将它添加到它们并不能解决问题。
如果您的解决方案中有 .NET Standard 类库或 w/e,则它们不需要使用相同的版本,尽管它们可能应该是您可以使用的最新版本。例如,我的 .NET Standard 类库是 2.2,因为还没有 .NET Standard 3.0。
没有一个答案,包括选中的答案对我不起作用。
解决方案要简单得多。我首先从我的 BUS 层中删除了引用。然后从项目中删除 dll(以确保它消失了),然后从 nuget packeges 重新安装 JSON.NET。而且,棘手的部分是“将其关闭再打开”。
我刚刚重新启动了视觉工作室,它在那里工作!
因此,如果您尽一切可能尝试但仍然无法解决问题,只需尝试关闭并重新打开 Visual Studio,它可能会有所帮助。
我通过在 bin 文件夹中删除较低版本的 Newtonsoft.json.dll 并添加 Newtonsoft.json.dll V6.0.0.0 并删除了以下 web.config 元素来解决它
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"/>
<bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="5.0.8"/>
</dependentAssembly>
我在 dotnet core 上遇到了同样的问题,并设法通过清除 NuGet 缓存来解决它。
打开powershell并输入以下命令。
dotnet nuget locals all --clear
然后我关闭了 Visual Studio,再次打开它并在包管理器控制台中输入以下命令:
Update-Package
NuGet 现在应该恢复所有包并再次填充 nuget 缓存。
之后,我能够在 Linux 容器中构建和启动我的 dotnet core webapi。
解决我的问题的解决方案是
转到引用->右键单击 Newtonsoft.json--转到属性并检查版本
这个相同的版本应该在
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-YourDllVersion" newVersion="YourDllVersion" />
</dependentAssembly>
简单的步骤。 (1)在包管理器中运行“update-package Newtonsoft.Json -reinstall”。
(2)通过启用查看隐藏文件并删除bin文件夹来删除您的bin。
(3)关闭您的视觉工作室并重新打开它。
(4) 现在再次运行您的项目。我相信应该没问题
我遇到了这个问题,因为我有:项目 A(桌面 .exe)引用项目 B(便携式 .dll)。 A & B 有不同版本的 JSON.Net,因此存在加载器冲突。一旦我使 JSON.net 的所有版本都相同,它就可以工作了。 (这实际上是上述一些解决方案正在做的事情——我只是在说明它为什么有效)
我最近在和较旧的项目中遇到了这个问题。
在检查了 bin 文件夹中实际存在的 dll 版本后,我设法找到了问题所在。
我有一个构建后脚本,它将依赖程序集从 dll 库文件夹复制到 bin 文件夹。 nuget 之前的常见设置。
因此,每次我构建构建后脚本时,都会将正确版本的 Json.net 替换为旧版本
我遇到了完全相同的问题,我的 Visual Studio 13 默认库是 4.5,所以我有 2 个解决方案,一个是在 webconfig 文件中删除对此的引用。这是最后的手段,它确实有效。
错误消息指出此位置 /Projects/foo/bar/bin/Newtonsoft.Json.DLL 存在问题。 DLL 在哪里!基本属性检查告诉我它是 4.5.0.0 或类似的,所以我将 webconfig 更改为查找 4.5 并使用 4.5。
这些选项都不适合我,最终它是;
测试 > 测试设置 > *.testrunconfig
我不得不添加一个新行
<DeploymentItem filename="packages\Newtonsoft.Json.4.5.8\lib\net40\Newtonsoft.Json.dll" />
确保路径和版本适合您的设置。
我遇到过同样的问题。我创建了一个 web api 项目,然后将其从 .net 4.5.1 升级到 .net 4.6.1。关闭 IDE、停止 IIS 并删除 C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\ 目录的内容修复了该问题。
高温高压
通过刷新 NuGet 包或对项目执行“清理”,我无法解决这个问题。它是通过手动清除“obj”文件夹来解决的。
不定期副业成功案例分享