Search code examples
c#.net-assemblyautodesk-navisworks

Load an assembly from plugin


INTRODUCTION

I'm writing a plugin for Navisworks and i'm using the Dropbox api to download/upload documents from a repository.

PROBLEM

Dropbox.Api uses the Newtonsoft.Json.dll version 7.0, the problem is that Navisworks uses the 4.0 version of the same assembly, so I cannot use the Dropbox api because it throws an exception every time:

System.AggregateException: One or more errors occurred. ---> System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=7.0.0.0, ...

So as I understand the program has the assembly 4.0v so Dropbox.Api cannot execute properly.

By now, what I've done is to use another process which I can load the right assembly and download/upload the files from there, but I would like to avoid using a second process.

I'm trying to use reflection to load the assembly at runtime, but it takes no effect, the program still cannot find the newer assembly.

//Load the assembly at the beginning of the plugin
var ass = System.Reflection.Assembly.Load(Properties.Resources.Newtonsoft_Json);

//Use the Dropbox api
//Exception...

Can I force, somehow, the program to use the newer assembly (temporary)?

Is there some solution that I've missed?


Solution

  • You are encountering this issue because you cannot load two different non-strong-named versions of a .NET assembly (no matter where it is on the file system) into the same AppDomain. By default you start with a single AppDomain called the Primary AppDomain for your process.

    A strong-named assembly is one that takes the filename; version; signing key and culture to produce a unique assembly.

    By now, what I've done is to use another process which I can load the right assembly and download/upload the files from there, but I would like to avoid using a second process.

    No need to create a second process, you can instead create a 2nd AppDomain in the same process. Each AppDomain can load different versions of assemblies including Newtonsoft.Json without conflict.

    I'm trying to use reflection to load the assembly at runtime, but it takes no effect, the program still cannot find the newer assembly.

    That won't work, it's essentially the same as letting .NET doing it for you automatically.

    The only time when you can load multiple versions of .NET assemblies into the same AppDomain is if the assembly (in this case the NuGet package) and dependent assemblies are all strong-named. For some reason I have never been able to fathom is why most open source .NET devs refuse to strong-name assemblies.