Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 291
Dave New Avatar asked Feb 19 '14 09:02

Dave New


3 Answers

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.

like image 93
BgRva Avatar answered Nov 13 '22 17:11

BgRva


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

like image 35
Dave New Avatar answered Nov 13 '22 17:11

Dave New


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.

like image 28
user3709150 Avatar answered Nov 13 '22 18:11

user3709150