60

I have a WinJS project that is previously built on Windows 8.1 using VS 2013.

Recently I upgraded this project to Universal Windows 10 by creating a blank Javascript Universal windows 10 project and then added all my files from old project.

I have Windows Runtime Components and also Class Library for SQLite.

I added Universal Windows Runtime Component and Universal Class Library and copied all my files from old project to respective places.

Somehow I managed to remove all the build errors.

I installed all the required SQLite-net, SQLite for Universal Windows Platform, Newtonsoft, etc.

But when I run the application and call a Native method in Windows Runtime Component it gives some kind of strange errors as:

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

Additional information: Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.

enter image description here

Newtonsoft version is: 9.0.1

My project.json file of Windows Runtime Component has following:

  {
  "dependencies": {
    "Microsoft.NETCore.UniversalWindowsPlatform": "5.0.0",
    "Newtonsoft.Json": "9.0.1"
  },
  "frameworks": {
    "uap10.0": {}
  },
  "runtimes": {
    "win10-arm": {},
    "win10-arm-aot": {},
    "win10-x86": {},
    "win10-x86-aot": {},
    "win10-x64": {},
    "win10-x64-aot": {}
  }
}

My Visual Studio version is:

enter image description here

I tried removing all the Newtonsoft json and re-installing it but no luck.

Kishor Bikram Oli
  • 1,748
  • 1
  • 22
  • 40
  • 3
    I had a similar problem with a new ASP.NET Core application a while ago. Turns out one of the referenced libraries used a version of Newtonsoft.Json that was lower than 9.0.0.0. So I upgraded the version for that library and the problem was solved. Not sure if you'll be able to do the same here. – hbulens Jul 30 '16 at 07:53
  • Hi @hbulens, I tried installing the latest version but gives the same error again and again. – Kishor Bikram Oli Jul 30 '16 at 09:24
  • May be this is related to your issue - https://github.com/aspnet/Home/issues/1609 – Sanket Jul 30 '16 at 09:58

24 Answers24

51

I solved this problem by adding Newtonsoft.Json to the NuGet of the startup project (even though it is not directly used in the startup project).

Feng Jiang
  • 1,776
  • 19
  • 25
  • yes...mine was also fixed after reinstalling from NuGet. – kplshrm7 Mar 09 '18 at 09:44
  • 4
    Legend! Not even referenced or required in the startup project (which is a console app), but fixed the issue in my class library once it was added – Googs Jul 23 '18 at 18:36
  • 7
    Daaanngg! HOURS and HOURS of my day wasted. Wish I had seen this answer a long time ago. Thank you! And what's weird is that I never had Newtonsoft.Json in the startup project, and it's been working just find for months. Then today, it decides to complain. – Casey Crookston May 31 '19 at 17:31
  • 2
    Worked for our ASP.NET Core 1.0.1 web app. Added Json.NET to our project where Startup class is located and it worked. Thanks. – ashilon Jul 09 '19 at 10:00
  • Still works in 2021 with Newtonsoft .Json 13.0.0.0 – Jane S Dec 20 '21 at 05:10
  • Worked for me, also installed the latest stable version – Gabe Apr 17 '22 at 19:11
  • This did not work for me. We use Newtonsoft.Json v11.0.1 in several DLLs that get dynamically loaded in a System.ComponentModel.Composition.Hosting.AggregateCatalog. It throws this error when ComposeParts() is called. This is a .Net 4.8 WPF test application tool. This is really irritating because the error does not tell you which file it has a problem with. Adding a .Nuget package reference to all referenced DLLs did not work. The only solution I found was to adding the section (see Varun's answer below) to to the .exe.config file (using newVersion=11.0.0.0 in my case). – pwrgreg007 Jun 09 '22 at 14:41
  • What's also confusing is that, on my system, there are two .Nuget package locations: and . It seems Microsoft has revived DLL hell (if it wasn't already resurrected vis-a-vis dotnet core). – pwrgreg007 Jun 09 '22 at 14:48
39

I made a basic Demo and reproduced this problem. It seems that WinRT component failed to find the correct assembly of Newton.Json. Temporarily the workaround is to manually add the Newtonsoft.json.dll file. You can achieve this by following steps:

  1. Right click References-> Add Reference->Browse...-> Find C:\Users\.nuget\packages\Newtonsoft.Json\9.0.1\lib\portable-net45+wp80+win8+wpa81\Newtonsoft.json.dll->Click Add button.

  2. Rebuild your Runtime Component project and run. This error should be gone.

Elvis Xia - MSFT
  • 10,801
  • 1
  • 13
  • 24
  • Im using MonoDevelop on linux mint and this worked for me, I used one of the portable ones, in one of the subfolders in the the `Newtonsoft.json.9.0.1/lib` folder – isebarn Dec 10 '16 at 15:58
  • I also had to remove the NuGet package reference. Otherwise, it wouldn't allow me to add another link to the .dll. – Artemious Sep 12 '17 at 12:44
21

I had the same issue too, to solve this, check in References of your project if the version of Newtonsoft.Json was updated (probablly don´t), then remove it and check in your either Web.config or App.config wheter the element dependentAssembly was updated as follows:

<dependentAssembly>
  <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>

After that, rebuild the project again (the dll will be replaced with the correct version)

Richard Lee
  • 2,136
  • 2
  • 25
  • 33
12

I am using Visual Studio 2013 Update 2. In my case, I have a web project and a Web Api project and unit test project and other class libraries in a single solution.

I've spent couple of days to solve the problem. Below is the step-by-step solution that I have found.

  1. Right click on Web Api project. Select "Set as StartUp Project"
  2. Right click on Web Api project. Go to Properties ( Alt + Enter ).
  3. On the Application tab on left hand side menu, select Application
  4. Find Target framework. Change it to 4.5.1 and save. However, it is showing error in "Error List" window. After Rebuild, there is no error.
  5. Remove all Newtonsoft.Json packs from solution by using below query from Package Manager Console ( to get it View > Other Window > Package Manager Console ).

uninstall-package newtonsoft.json -force

  1. Reinstall Newtonsoft.Json from Package Manager Console

install-package newtonsoft.json

  1. If you have latest update for Visual Studio 2013, you might not encounter with this problem. As I am using Update 2, so, while trying to install Newtonsoft.Json, I have encountered with the following error.

The 'Newtonsoft.Json 10.0.3' package requires NuGet client version '2.12' or above, but the current NuGet version i s '2.8.50313.46'

  1. To solve this problem, we need to update the Package Manager Console. Got to

Tools > Extensions and Updates... > In left pane.. select Updates > Visual Studio Gallery.

  1. Update the NuGet Package Manager Extension. Follow the steps that are coming afterwards.

  2. Visual Studio will take a restart after that.

  3. Execute step 6 again.

After Installation packages.config will be added with this below line

  <package id="Newtonsoft.Json" version="10.0.3" targetFramework="net451" />

After installation web.config will be added with this below lines

<dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
</dependentAssembly>

It will execute successfully, if there is no other error.

Kuntal Ghosh
  • 3,548
  • 2
  • 17
  • 21
7

I had a similar problem with a new ASP.NET Core application a while ago. Turns out one of the referenced libraries used a version of Newtonsoft.Json that was lower than 9.0.0.0. So I upgraded the version for that library and the problem was solved. Not sure if you'll be able to do the same here

hbulens
  • 1,872
  • 3
  • 24
  • 45
6

Adding binding redirect configuration for Newtonsoft.Json in your configuration file (web.config) will resolve the issue.

<configuration>
    <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>
        </assemblyBinding>
    </runtime>
</configuration>

Since Newtonsoft.Json version in your case is 9 update the version appropriatly in the configuration.

If this configuration does not work make sure the namespace (xmlns) in your configuration tag is correct or remove the name space completely.

Assembly binding redirect does not work

Varun
  • 422
  • 3
  • 14
5

I think AutoCAD hijacked mine. The solution which worked for me was to hijack it back. I got this from https://forums.autodesk.com/t5/navisworks-api/could-not-load-file-or-assembly-newtonsoft-json/td-p/7028055?profile.language=en - yeah, the Internet works in mysterious ways.

// in your initilizer ...
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);

.....

private Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
{
    if (args.Name.Contains("Newtonsoft.Json"))
    {
        string assemblyFileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\Newtonsoft.Json.dll";
        return Assembly.LoadFrom(assemblyFileName);
    }
    else
        return null;
}
CAD bloke
  • 8,578
  • 7
  • 65
  • 114
  • Great stuff, especially useful when working with tons of dependencies and one of them wants some version which is not available (Newtonsoft.Json 11.0.0 as of this moment). – Vitalii Vasylenko Apr 15 '19 at 11:47
  • When editing app.config or web.config doesn't work, this is a great solution esp. for Newtonsoft.Json, where every library you use references an older version of this fucking package – Ambrose Leung Jan 12 '21 at 19:39
3

It took us a day to resolve this problem. The solution is forcing your webservice to use version 11.0.0 in your web.config file.

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

Sabri Meviş
  • 2,231
  • 1
  • 32
  • 38
3

I had same issue with the following version 12.0.3:

Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.

This issue was only in my Test project (xUnit) and was caused by lack of newtonsoft.json in this project.

What is important, I was testing code from another project where library was attached and works properly.

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
LucasK
  • 31
  • 3
2

I've experienced similar problems with my ASP.NET Core projects. What happens is that the .config file in the bin/debug-folder is generated with this:

  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="6.0.0.0" newVersion="9.0.0.0" />
    <bindingRedirect oldVersion="10.0.0.0" newVersion="9.0.0.0" />
  </dependentAssembly>

If I manually change the second bindingRedirect to this it works:

<bindingRedirect oldVersion="9.0.0.0" newVersion="10.0.0.0" />

Not sure why this happens.

I'm using Visual Studio 2015 with .Net Core SDK 1.0.0-preview2-1-003177.

HGHansen
  • 41
  • 3
2

In my case it was an issue with the configuration file web.config on my machine when I updated the newton version VS automatically fixed my web.config file to point to the new version. When I uploaded it to production the existing web.config was pointing to the old version.

Once I updated the web.config it started working again.

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
  </dependentAssembly>
AmirK
  • 21
  • 2
2

One need to update Newtonsoft.Json -Version GO to Tools => NuGet Package Manager => Package Manager Console and Type Install-Package Newtonsoft.Json -Version 12.0.2 in Package Manager Console Window.

2

I had a very similar problem. I was trying to use Newtonsoft.Json.dll in a .NET DLL, in the same way that I am successfully using it in .NET EXEs on my computer. I used NuGet in my Visual Studio 2017 to add Newtonsoft.Json to MyDll.dll. MyExecutable.exe references MyDll.dll. Calling a Newtonsoft.Json method from code within MyDll.dll raised "System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=12.0.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)".

I ran Microsoft's fuslogvw.exe https://learn.microsoft.com/en-us/dotnet/framework/tools/fuslogvw-exe-assembly-binding-log-viewer to check what was being loaded and found the following:

LOG: Post-policy reference: Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/MyExecutable/bin/Debug/Newtonsoft.Json.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\MyExecutable\bin\Debug\Newtonsoft.Json.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: The assembly reference did not match the assembly definition found.
ERR: Run-from-source setup phase failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

MyExecutable.exe had no references or calls to Newtonsoft.Json, yet I found a 6.0.0.0 Newtonsoft.Json.dll in bin\Debug directories of copies of my MyExecutable source tree from before I added any Newtonsoft.Json references to any of my code. I do not know why the 6.0.0.0 Newtonsoft.Json.dll was there. Perhaps it was referenced by another DLL referenced by MyExecutable. I avoided the FileLoadException by using NuGet to add a reference to 12.0.0.0 Newtonsoft.Json to MyExecutable.

I expected that binding redirect in MyExecutable’s App.config as illustrated below would be an alternative to MyExecutable’s referencing Newtonsoft.Json, but it did not work. …

Alan Barksdale
  • 153
  • 1
  • 6
2

I was facing this issue when I added Nuget package Newtonsoft.Json 12.0.0.2 into my two .netstandard library projects and it grabbed almost my full day to solve this issue.

Exception -: Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'. The system cannot find the file specified.

Solution -: I had to remove package from Nuget and went to the following location and followed the next steps -

Step 1. Go to location "C:\Users[UserName].nuget\packages\newtonsoft.json\12.0.2\lib" and here you will get all Nuget versions you installed previously.

enter image description here

Step 2. Since i wanted to use it in my .netstandard 2.0 library project, so i copied "netstandard2.0" folder from this location and paste somewhere my preferred location (**where i generally keep 3rd party dlls).

Step 3. Now i added DLL reference from here to my both project and this way problem solved.

Thanks

Rahul
  • 1,063
  • 2
  • 11
  • 22
2

I was not able to solve it by any of the options mentioned below. After doing some research, looking into lot of resources and taking help from one of my colleague - I was able to solve it by manually registering the Newtonsoft.Json.dll in GAC. Below is the command I used to do this:

gacutil /i "C:\Users\username\path\to\my_project\packages\Newtonsoft.Json.13.0.1\lib\net20\Newtonsoft.Json.dll"

Additional References:

Gangula
  • 5,193
  • 4
  • 30
  • 59
2

In 2022, I fixed this issue by adding the following two lines to my csproj file, within the first PropertyGroup. This was after I migrated from packages.config to the PackageReference style of csproj files.

<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

I was hoping to avoid maintenance of the app.config files. This was for a XF application, so I'm not sure if this works for web applications.

This advice came from:

Hope this helps someone!

Chris Sears
  • 311
  • 3
  • 2
1

Was able to solve this problem in my asp.net mvc project by updating my version of Newton.Json (old Version = 9.0.0.0 to new Version 11.0.0.0) usign Package Manager.

David Zagi
  • 1,383
  • 2
  • 10
  • 18
1

It is possible to solve the problem by updating the 'Newtonsoft' version.

In Visual Studio 2015 it is possible to right click on the "Solution" and select "Manage Nuget packages for solution", search for "Newtonsoft" select a more current version and click update.

1

I solved this issue by removing all the NuGet packages from solution and reinstalling them. One of the NuGet Packages was dependent upon NewtonSoft and it was not showing in references

1

Remove all BIN and OBj folders from all projects of that solution. Also, Update the package for All projects when it's used. Remove Package folder and rebuild and run. It worked for me.

Darshit Gandhi
  • 121
  • 1
  • 7
1

I had a similar problem with a new Project. Check that the reference versions are the same in affiliate projects.

saeid mohammad hashem
  • 1,271
  • 1
  • 15
  • 19
1

I was getting the same error:

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

I uninstall the Newtonsoft.Json and install this version.

enter image description here

It works for me.

Mohammad Atiour Islam
  • 5,380
  • 3
  • 43
  • 48
1

An old post, but still relevant. Hopefully this helps someone. I had the same issue, and it was caused by an indirect reference from a library that my project referenced.

You can solve this by telling your app how to resolve it by adding code to App.xaml.cs (or where ever you assembly is initialized).

public App()
{
    AppDomain.CurrentDomain.AssemblyResolve += (s,a) => CurrentDomain_AssemblyResolve(s,a);
}

private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    Assembly myAssembly = null;
    if (args.Name.Contains("Newtonsoft.Json,"))
    {
        myAssembly = Assembly.LoadFile(Path.Combine(ExecutingDirectory, "Newtonsoft.Json.dll"));
    }
    return myAssembly;
}
Nik
  • 1,780
  • 1
  • 14
  • 23
1

I had the same issue with v13.0.0.0 no matter what I did I couldn't get it to go away. I re-installed Newtonsoft.Json restarted the program and also tried different versions of Newtonsoft.Json. The thing that worked was updating Visual Studio.