简体   繁体   English

FileLoadException未被用户代码处理

[英]FileLoadException was unhandled by user code

I am setting up the API for my MVC-4 app and when I uncommented this line in Globals.asax.cs: 我正在为我的MVC-4应用程序设置API,当我在Globals.asax.cs中取消注释这一行时:

WebApiConfig.Register(GlobalConfiguration.Configuration);

I received this exception when I started my project back up: 当我重新启动项目时,我收到了这个异常:

An exception of type 'System.IO.FileLoadException' occurred in mscorlib.dll but was not handled in user code

Additional information: Could not load file or assembly 'Newtonsoft.Json, Version=4.5.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)

What should I do? 我该怎么办?


Update 1 (screenshots) 更新1(截图)

from what I can tell, JSON.Net looks like it is installed correctly. 据我所知,JSON.Net看起来已正确安装。

在此输入图像描述

在此输入图像描述

在此输入图像描述


Update 2 更新2

JSON.Net actually seems to work when the API routes are commented out in Globals.Asax. 当在Globals.Asax中注释掉API路由时,JSON.Net实际上似乎有效。 This doesn't throw any errors: 这不会引发任何错误:

public ActionResult Index()
{
     var foo = Newtonsoft.Json.JsonSerializer.Create();
     return View();
}

Visual Studio only complains when this line is uncommented: Visual Studio仅在取消注释此行时才会抱怨:

WebApiConfig.Register(GlobalConfiguration.Configuration);

This also occured to me today. 这也发生在我今天。 Seems like there had been an update for json.net (now version 6.0.3), causing nuget to download the latest version after build. 似乎有一个json.net(现在的版本6.0.3)的更新,导致nuget在构建后下载最新版本。 However references to old json.net libs might not get updated when there are depencies to other libs. 但是,当与其他库存在依赖关系时,对旧json.net库的引用可能不会更新。

Solution: Manually open the manage nuget packages for solution window and uninstall old version(s) of json.net. 解决方案:手动打开解决方案窗口的manage nuget包并卸载json.net的旧版本。 Then take the latest version and install for all needed projects. 然后获取最新版本并安装所有需要的项目。 That fixed the exact error you had for me... 这解决了你对我的确切错误......

-- edit -- - 编辑 -
Ok, so I found out that this solution worked for me locally, but remotely this did not solve my issues. 好的,所以我发现这个解决方案适合我本地,但是远程这并不能解决我的问题。 Seems like there are some old dependencies from other libs hard referencing the 4.5.0.0 version of json.net. 似乎有一些来自其他库的旧依赖项很难引用json.net的4.5.0.0版本。 More topics on Stackoverflow.com provide the following solution. Stackoverflow.com上的更多主题提供以下解决方案。

Add this assembly binding redirect to your web.config file: 将此程序集绑定重定向添加到web.config文件:

<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
                <bindingRedirect oldVersion="1.0.0.0-4.5.0.0" newVersion="6.0.0.0"/>
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

Most probably Newtonsoft.Json DLL is not properly deployed. 很可能Newtonsoft.Json DLL没有正确部署。

Make sure you have the Newtonsoft.Json DLL in your (IIS / project) bin folder. 确保您的(IIS /项目) bin文件夹中有Newtonsoft.Json DLL。
Alternatively, you can also install that DLL to GAC if you plan to use it across multiple projects. 或者,如果您计划在多个项目中使用该DLL,也可以将该DLL安装到GAC。

It looks like that you don't have Newtonsoft.Json installed/referenced. 看起来你没有安装/引用Newtonsoft.Json。 Web API relies on this and won't work correctly until you resolve this dependency. Web API依赖于此,在解决此依赖关系之前无法正常工作。 You can install it via NuGet . 您可以通过NuGet安装它。

Simply delete your Newtonsoft.Json dll from bin folder then open package.config file and remove your Newtonsoft.Json entry from there then reinstalled your Newtonsoft.Json by command but don't installed newer version if you face this problem with newer version find old version command 只需从bin文件夹中删除你的Newtonsoft.Json dll,然后打开package.config文件,然后从那里删除你的Newtonsoft.Json条目,然后通过命令重新安装你的Newtonsoft.Json,但如果你遇到这个问题,请不要安装更新的版本版本命令

like Install-Package Newtonsoft.Json -Version 6.0.8 now Install-Package Newtonsoft.Json -Version 7.0.1 is also aviable but i suggest to you installed 6.0.8 version its working 像Install-Package Newtonsoft.Json -Version 6.0.8现在安装包Newtonsoft.Json -Version 7.0.1也可以,但我建议你安装6.0.8版本它的工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM