Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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.

enter image description here

enter image description here

enter image description here


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);
like image 240
drewwyatt Avatar asked May 05 '14 20:05

drewwyatt


1 Answers

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>
like image 127
Hutjepower Avatar answered Sep 28 '22 02:09

Hutjepower