Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not load file or assembly Newtonsoft.json.dll

“Could not load file or assembly 'Newtonsoft.Json, Version=4.0.3.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)”.

'NewtonsoftJson.dll’ assembly is required for posting to twitter .The version used is 4.0.3.0.

And the same assembly(but diff version 4.0.0.0) is used as the dependent assembly by facebook C# api(dll). However the above assembly(4.0.3.0) does not work for both the cases (ie for posting to twitter and for login to facebook). In order to solve it ,created a separate project for posting to twitter and reference the assembly(4.0.3.0) separately (in the project for twitter post).And another version(4.0.0.0) was added as reference in main project for facebook but still the error comes.If twitter project is disabled and ran then the facebook login works fine and vice vera.

I have done lot of research and tried the following.

delete temporary asp.net files clean solution restart computer

Even tried to uninstall the assembly from gac(however its not registered there).

Please help me on this as its not working. Thanks, S

like image 521
Sagar Avatar asked Feb 24 '12 07:02

Sagar


2 Answers

It's not clear what the relationships of the projects are, or when this error is occuring, but here's a guess.

You have 3 projects the facebook project (refs 4.0.0.0 version), twitter project (refs 4.0.3.0 version), and a main project that refs both of those projects. You may be able to build this solution, but when you run the assembly binding will fail. Why? because the default behavior is to copy assemblies locally before running. What happens is that the first project to build copies to bin (say twitter) then the second project builds (facebook), then main. However, at the end of this the 4.0.0.0 version is sitting bin folder. When you run, as soon as you invoke something from twitter that tries to use the problem assembly the bind fails because it longer has access to the 4.0.3.0 version of the assembly.

There are a couple of ways around this. One is register both assemblies in the GAC. If that isn't doable then look into assembly binding redirection in your config file. Another is to register for the AssemblyResolve event and load the assembly programmatically.

like image 153
Mike Zboray Avatar answered Nov 14 '22 15:11

Mike Zboray


Add the following to your app.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-4.5.0.0" newVersion="4.5.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

within your <configuration></configuration> tags

like image 8
Ayana Avatar answered Nov 14 '22 16:11

Ayana