简体   繁体   中英

Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies

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.

在此处输入图像描述

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:

在此处输入图像描述

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

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.

我通过在启动项目的NuGet中添加Newtonsoft.Json解决了这个问题。

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)

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 is '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.

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

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

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

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.

需要将Newtonsoft.Json -Version GO 更新到 Tools => NuGet Package Manager => Package Manager Console 并在 Package Manager Console 窗口中键入 Install-Package Newtonsoft.Json -Version 12.0.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://docs.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.

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.

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>

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.

在此处输入图片说明

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

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

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.

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>

通过使用包管理器更新我的 Newton.Json 版本(旧版本 = 9.0.0.0 到新版本 11.0.0.0),能够在我的 asp.net mvc 项目中解决这个问题。

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.

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

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.

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

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:

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.

在此处输入图像描述

It works for me.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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