Community
Navisworks API
Welcome to Autodesk’s Navisworks API Forums. Share your knowledge, ask questions, and explore popular Navisworks API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Could not load file or assembly Newtonsoft.Json

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
eceballostorres
7927 Views, 5 Replies

Could not load file or assembly Newtonsoft.Json

I have created a plug-in that uses Newtonsoft.Json to handle Json format. When I compile my code everything works fine but when I run the plug-in I get the following runtime error:

 

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

 

I have checked that Newtonsoft.Json exists in the Dependencies folder so that wouldn't be the problem. I believe the problem is that Navisworks is trying to load version 10.0.0.0 (specifically that version) while the dll version is actually 10.0.2.

 

For some reason when I add Newtonsoft.Json as a reference in my project instead of displaying "Version 10.0.2" it displays "Version 10.0.0". Please see below picture.

 

newtonsoft.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

I'm quite sure that it has something to do with Navisworks because the exact same code works in a Revit plug-in.

 

Any help will be very much appreciated.

Thanks.

5 REPLIES 5
Message 2 of 6
god_xp
in reply to: eceballostorres

you can try this

 

var assemblyFileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\Newtonsoft.Json.dll"

Assembly.LoadFrom(fileName);

Tags (1)
Message 3 of 6
eceballostorres
in reply to: god_xp

Thanks for your reply god_xp, I can try your suggestion.

Message 4 of 6
ken
Advocate
in reply to: eceballostorres

Ran into this *VERY* irritating situation myself and want to share how to research and fix this so that nobody else has to suffer at the hands of the CLR assembly loader without knowing how to track it down. 

 

1. You need Fuslogvw figure out how to get it. https://docs.microsoft.com/en-us/dotnet/framework/tools/fuslogvw-exe-assembly-binding-log-viewer

2. You need to enable assembly loading logging in the registry (you can try through the settings of fuslogvw but it didn't work for me and I needed to add it to the registry:  https://stackoverflow.com/a/1527249/5410501

3. Run your app and review the log for the assembly in question.  What we find for Navisworks and Newtonsoft is that the first place the CLR looks after the GAC is the Navisworks directory!  and low and behold, it finds newtonsoft.json version 6!

LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Program Files/Autodesk/Navisworks Manage 2017/Newtonsoft.Json.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Program Files\Autodesk\Navisworks Manage 2017\Newtonsoft.Json.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
WRN: Comparing the assembly name resulted in the mismatch: Major Version

Then you get a file not found exception.  Bummer.

 

You cannot simply try to load the assembly with an Assembly.Load() call because it doesn't care if the assembly is already loaded, it looks again from scratch when it needs it.  (double bummer)

 

What you need to do is hook up an AssemblyResolver Event handler to load the correct version of the DLL and return it.  Here is the relevant code:

 

AppDomain currentDomain = AppDomain.CurrentDomain;

currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
private Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) { if (args.Name.Contains("Newtonsoft")) { string assemblyFileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\Newtonsoft.Json.dll"; return Assembly.LoadFrom(assemblyFileName); } else return null; }

I wanted mine pathed to right next to my addin dll but you can path it wherever you like.  The Name property also includes the version so you can do more specific resolution if necessary.

 

more info on the Resolve event handler:

https://docs.microsoft.com/en-us/dotnet/framework/app-domains/resolve-assembly-loads

 

 

I hope no-one in this group every has to fight with newtonsoft again, or any other CLR assembly load failures.

 

BTW, you *could* also just load up newtonsoft version 6 for your addin (if you like) because when it finds it in the Navisworks directory, it will all be good too.  Just depends on whether you want to run ver 6 or if you really want to run ver 10.

 

cheers,

-Ken

Message 5 of 6
kzemlickas
in reply to: ken

Saved my day! Thank you very much.

Message 6 of 6

Thank you very much for this solution. It did hit me too!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Rail Community


 

Autodesk Design & Make Report