Dave New
Dave New

Reputation: 40092

Assembly reference cannot be resolved - dependentAssembly issue?

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.

Upvotes: 20

Views: 18720

Answers (5)

Marcus Ki&#248;nig
Marcus Ki&#248;nig

Reputation: 11

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.

Upvotes: 0

SharpC
SharpC

Reputation: 7464

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.

Upvotes: 0

user3709150
user3709150

Reputation: 31

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.

Upvotes: 3

BgRva
BgRva

Reputation: 1551

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.

Upvotes: 15

Dave New
Dave New

Reputation: 40092

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

Upvotes: 14

Related Questions