FileLoadException是由用户code未处理 [英] FileLoadException was unhandled by user code

查看:194
本文介绍了FileLoadException是由用户code未处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置为我的MVC-4应用程序中的API,当我在Globals.asax.cs注释掉这一行:

  WebApiConfig.Register(GlobalConfiguration.Configuration);

我收到此异常,当我开始我的项目备份:

 键入System.IO.FileLoadException的异常出现在mscorlib.dll但在用户code没有处理其他信息:无法加载文件或程序集Newtonsoft.Json,版本= 4.5.0.0,文化=中性公钥= 30ad4fe6b2a6aeed'或它的一个依赖。找到的程序集清单定义不匹配程序集引用。 (异常来自HRESULT:0x80131040)

我应该怎么办?


更新1(截图)

这是我可以告诉,JSON.Net看起来是正确安装。


更新2

JSON.Net实际上似乎当API航线Globals.Asax都注释掉工作。这不抛出任何错误:

 公众的ActionResult指数()
{
     无功富= Newtonsoft.Json.JsonSerializer.Create();
     返回查看();
}

Visual Studio中,只有当这一行注释掉抱怨:

  WebApiConfig.Register(GlobalConfiguration.Configuration);


解决方案

这也是今天发生在我身上。好像有曾是json.net(现在的6.0.3版)的更新,造成的NuGet版本后下载最新版本。但是老json.net库引用可能不会被当有depencies其他库更新。

解决方法:手动打开管理的NuGet包解决方案的窗口和卸载json.net的旧版本(S)。然后采取最新版本并安装所有需要的项目。该固定你对我有确切的错误...

- 编辑 - 的结果
好了,我发现这个解决方案为我在本地,但远程这并没有解决我的问题。好像有来自其他库硬引用4.5.0.0版本json.net一些旧的依赖。在Stackoverflow.com更多主题提供了以下解决方案。

将此程序集绑定重定向到您的的web.config 文件:

<结构>
    <&运行GT;
        < assemblyBinding的xmlns =瓮:架构 - 微软COM:asm.v1>
            < dependentAssembly>
                < assemblyIdentity名称=Newtonsoft.Json公钥=30ad4fe6b2a6aeed文化=中性/>
                < bindingRedirect oldVersion =1.0.0.0-4.5.0.0NEWVERSION =6.0.0.0/>
            < / dependentAssembly>
        < / assemblyBinding>
    < /运行>
< /结构>

I am setting up the API for my MVC-4 app and when I uncommented this line in 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)

from what I can tell, JSON.Net looks like it is installed correctly.


Update 2

JSON.Net actually seems to work when the API routes are commented out in Globals.Asax. 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:

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. However references to old json.net libs might not get updated when there are depencies to other libs.

Solution: Manually open the manage nuget packages for solution window and uninstall old version(s) of 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. More topics on Stackoverflow.com provide the following solution.

Add this assembly binding redirect to your web.config file:

<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>

这篇关于FileLoadException是由用户code未处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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