Problems loading dynamic assembly dependencies at runtime

Let me try to explain my problem. I am currently trying to develop a small “plugin framework” written in .Net (mainly for experimentation). Therefore, the idea is to have a main application to which you can add “plugins” by deploying a DLL in a specific “plugins” folder of the main application. Everything works fine, plugins are created correctly, but now I am faced with a problem. Now I have deployed the “X” plugin, which uses additional third-party plugins, and now I have a problem that these additional third-party plugins required by “X” were not found at runtime. My idea is now to add additional “dependencies” to the directory, where I also deploy all the necessary plugins.

So, my first question is: How can I upload assemblies to the application domain (given that I know the path to them) st can they be used by my application?

I tried to approach this by doing something like:

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { //find the path to the assembly and then load and return it by //return Assembly.Load("pathToDependencies/failedAssembly.dll"); } 

The problem is that this event handler is now activated using "Presentation.Zune.dll" in the args variable (I am using a WPF application). It seems that the build failed, but another dll is the actual problem.

Can someone suggest me a better way to solve my problem? I hope that I was able to explain my situation in sufficient detail, otherwise I’ll just ask for further clarification.

Thanks, Juri

+4
source share
3 answers

You can set the runtime check path so that it can find assemblies. Set the item in the application configuration file.

 <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <probing privatePath="plugins;dependencies"/> </assemblyBinding> </runtime> </configuration> 
+9
source

The AssemblyResolve event occurs when the environment tries to load an assembly and does not work.

This means that if it gives you Presentation.Zune.dll in args, that the infrastructure cannot find this assembly, and this is your chance to intercept it and do other things, for example, load it from a directory, the Framework may not know about - for example, your plugins \ dependencies folder ...

On top of my head, I would try something like this:

 Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { if( File.Exists(".\\Plugins\\"+args.Name) ) // it a plugin return Assembly.Load(".\\Plugins\\"+args.Name); else if( File.Exists(".\\Plugins\\Dependencies\\"+args.Name) ) // it a dependency OF a plugin return Assembly.Load(".\\Plugins\\Dependencies\\"+args.Name); else throw new Exception(); } 
+2
source

BTW: You can significantly speed up assembly of assemblies by caching your assemblies that you have already enabled in the dictionary. If A depends on B, C and B depends on C and you load A, AssemblyResolve will be called twice for C, and loading the assembly only once is faster :)

(I'm not sure that this is always the case that AssemblyResolve is called more than once, but I noticed this when debugging the project once. And it won't hurt to cache assemblies ...)

+1
source

All Articles