5
votes

This one error is driving me nuts.

I installed the SendGrid NuGet package in one of my class libraries named BaseServices, which has a dependency on Newtonsoft.Json v7.0.1, so it installs that in my packages folder and references that.

In the class library, I have this binding redirect:

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

In the web.config of the ASP.NET MVC application, i.e. the client application that uses my class library, I have an assembly binding redirect for versions less than v6 to point to v6 of the Newtonsoft.Json library like so:

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

When I run my email sending code that is in the BaseServices library, I get this error:

The thread 0x1a4c has exited with code 0 (0x0). System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=7.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) File name: 'Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' at SendGrid.Helpers.Mail.Mail.Get() at BaseServices.EmailService.SendAsync(EmailMessage message) in MyFolder\BaseServices\EmailService.cs:line 39

=== Pre-bind state information === LOG: DisplayName = Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed (Fully-specified) LOG: Appbase = file:///MyFolder/Web/ LOG: Initial PrivatePath = MyFolder\Web\bin Calling assembly : SendGrid, Version=7.0.3.0, Culture=neutral, PublicKeyToken=4f047e93159395ca. === LOG: This bind starts in default load context. LOG: Using application configuration file: MyFolder\Web\web.config LOG: Using host configuration file: C:\Users\computer\Documents\IISExpress\config\aspnet.config LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config. LOG: Post-policy reference: Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed LOG: Attempting download of new URL file:///C:/Users/computer/AppData/Local/Temp/Temporary ASP.NET Files/root/ef9cacdf/e639667a/Newtonsoft.Json.DLL. LOG: Attempting download of new URL file:///C:/Users/computer/AppData/Local/Temp/Temporary ASP.NET Files/root/ef9cacdf/e639667a/Newtonsoft.Json/Newtonsoft.Json.DLL. LOG: Attempting download of new URL file:///MyFolder/Web/bin/Newtonsoft.Json.DLL. WRN: Comparing the assembly name resulted in the mismatch: Major Version ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

3

3 Answers

13
votes

You have 2 different versions of JSON.NET library in your solution. To solve this you should upgrade them to latest version. Follow these steps:

  1. Open solution explorer
  2. Right Click on solution name
  3. Select Manage Nuget Packages for Solution
  4. Select Updates from menu
  5. Update JSON.NET package

This will resolve your issue.

1
votes

I've had this craziness happen to me quite a bit. You need to make sure the Newtonsoft.Json assembly version is consistent in these places:

  • Project References (dll version)
  • Web.config
  • Packages.config
1
votes

I had the same exception message and came upon this thread. Hopefully, the following will help someone else.

My solution contains multiple projects. One of these projects uses the Microsoft.AspNet.WebApi.Client. One of its dependencies is the Newtsonsoft.Json library.

However, the library version that the Microsoft.AspNet.WebApi.Client leverages is version 6.0.

The problem then is that, at some point, the developer is informed of updates. So we update the JSON library. All is good, right?

We then add a setup project which creates an .msi installer. Ahhhh... in this .msi, we now see two version of the JSON library. Huh? How did that happen. Of course, we remove the old one.

Then, at some point, during testing, we get the exception message about version 6.0. But, now, as we wade through .config files, etc. we see references to version 11.0+.

I've seen various fixes on here from various threads. Some talk about forced uninstalls of the library and removal of the XML references in these .config files.

What worked for me was uninstalling the Microsoft.AspNet.WebApi.Client along with the JSON library and reinstalling the Microsoft.AspNet.WebApi.Client. The latter brings back its proper dependency.

Lesson: if you see multiple versions of a .dll in your setup project, be sure you know which one needs to be removed.

What is confusing, though, is that after upgrading the library, in debug mode, the solution continues to run without incident. It's only when I went to test deploy that I ran into the issue.

What's more, upgrading as the first answer suggests, was not the solution for me.