20

I have the following errors occurring on my build server (TFS/Visual Studio Online):

CA0055 : Could not load C:\a\Binaries\Api.dll. The following error was encountered while reading module 'System.Net.Http.Formatting': Assembly reference cannot be resolved: Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed.
CA0058 : The referenced assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' could not be found. This assembly is required for analysis and was referenced by: C:\a\Binaries\Api.dll, C:\a\Sources\MyLocation\packages\Microsoft.AspNet.WebApi.Client.5.1.1\lib\net45\System.Net.Http.Formatting.dll.

Here is the web.config dependentAssembly entry in my Api.dll project for this assembly:

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

The actual version of the installed Json.NET NuGet package is 6.0.1:

enter image description here

When looking in the project references, I have the Newtonsoft.Json as 6.0.0.0:

enter image description here

The version of System.Net.Http.Formatting in references is 5.1.0.0.

NuGet restore is enabled in the build definition and I do not have these errors on my local copy, only in TFS.

Is anyone able to spot what could be the problem?

I think it might be due to the dependentAssembly entry but I cannot get it to work.

Dave New
  • 38,496
  • 59
  • 215
  • 394
  • To diagnose binding errors, please use the Fusion Viewer: http://msdn.microsoft.com/en-us/library/e74a18c4(v=vs.110).aspx. – Polyfun Feb 19 '14 at 10:12
  • @Polyfun I tried fuslogvw, and it does absolutely nothing. An empty dialog and couldn't get it to do anything. – Alan Baljeu Apr 28 '22 at 14:04

5 Answers5

15

If you have scrubbed your project files, package files, and references and all versions are the correct and latest version of Newtonsoft, it could be a .Net dll with a dependency to an earlier version of Newtonsoft.Json. In my case it was System.Net.Http.Formatting, Version=4.0.0.0:

enter image description here

Try adding the following to the *.config of the calling project:

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

When running a test project against the WebAPI project, a FileNotFound exception was being thrown from the WebAPI because of a Newtonsoft.Json version mismatch between 4.5.0.0 and 6.0.1.0. Adding the statement to the app.config of the calling test project fixed the issue.

BgRva
  • 1,521
  • 12
  • 26
  • "Adding the statement to the app.config of the calling test project fixed the issue." this resolved my issue completely. In the project which used System.Net.Http.Formatting redirect was already being set, but it wasn't enough. Each other project which calls the project must have this redirect as well. – Zoran P. Sep 20 '15 at 08:37
  • Just for reference the `` tag is nested in `configuration` → `runtime` → `assemblyBinding` https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/dependentassembly-element – SharpC Mar 19 '19 at 09:34
  • My calling project doesn't have any config file? Other projects do, but not this one. – Alan Baljeu Apr 28 '22 at 14:06
14

The issue was something unexpected.

The fix was to include the following line in the project file under each relevant <PropertyGroup> section:

<CodeAnalysisAdditionalOptions>/assemblyCompareMode:StrongNameIgnoringVersion</CodeAnalysisAdditionalOptions>

To edit the project file, right click on the project and click on Unload Project. Now right click on the unloaded project and choose Edit MyProject.csproj

Dave New
  • 38,496
  • 59
  • 215
  • 394
  • 3
    I've got the same problem on our test server but it works fine on my dev machine and the .. does not work for me. Also tried StrongNameIgnoringVersion but no luck. This has cost me days. Any other ideas? – CRG Apr 22 '14 at 08:58
  • 1
    This saved my day. It's the way to go when you cannot modify Code Analysis' configuration directly (in corporate build servers it's usually that way) – SuperJMN Oct 07 '14 at 13:50
3

in my case the Newtonsoft.Json bindingredirect wasn't working because somehow the root web.config file was not part of the deployed files.

Check the properties of your web.config file. I our case, the "Build Action" value was set to "None". It should be set to "Content" to be part of the deployed files to the server.

Also explains why the website was working on (local) IISexpress but not on the full IIS instance.

0

I found that despite the class library I was creating having a reference to both System.Net.Http.Formatting and also Newtonsoft.Json, only the former was being copied to the bin directory of the calling project that needed it.

Adding a reference to Newtonsoft.Json to the main calling project fixed the issue I was getting:

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in System.Net.Http.Formatting.dll. Additional information: 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.

SharpC
  • 6,974
  • 4
  • 45
  • 40
0

I had the same issue, but on my local developer machine in an very old Web Site solution. The issue was that there was old "residue" in the web.config from .Net framework pre version 4.

So had to change From: To:

In other words, remove the part in bold appliesTo="v2.0.50727", without this change you get: Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0.

Struggled with this for days, trying a lot of different solutions, but none worked and this tread was the closest I found to discussing it. Obviously this could effect any assemblybinding, not just Newtonsoft. Hope this helps someone else.