FileLoadException was unhandled by user code

25,705

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>
Share:
25,705
drewwyatt
Author by

drewwyatt

Typed language enthusiast, Texas native, and obsessive automator.

Updated on January 31, 2020

Comments

  • drewwyatt
    drewwyatt over 4 years

    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);
    
  • drewwyatt
    drewwyatt almost 10 years
    That's the thing, the NuGet package manager shows it is installed, and the path listed in References looks like it points to the DLL on my file system. I'll update my post with screenshots.
  • drewwyatt
    drewwyatt almost 10 years
    Holy cow. You nailed it. Thanks!!
  • Rap
    Rap almost 10 years
    Unbelievable. How do we keep letting Microsoft get away with kludges like this? #smh
  • Patrick Borkowicz
    Patrick Borkowicz almost 9 years
    Thank you! It's July 2015 now and I had to change to oldVersion="1.0.0.0-6.0.0.0" and newVersion="7.0.0.0".
  • ArCiGo
    ArCiGo almost 8 years
    It's July 2016 and it worked for me, apparently. Thanks!
  • Shawn J. Molloy
    Shawn J. Molloy about 7 years
    Happy 2017! oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0"
  • VictorySaber
    VictorySaber about 7 years
    I had to add this to my App.Config in my unit test project and it then worked. The service layer was blowing up, saying it wanted Newtonsoft 4.5... I think because Http.Formatting was being used. This solved it.