I am currently working on a project which is supposed to work as a framework for several Add-Ons, which should be loaded at runtime.
I am tasked to have following structure in my application folder:
System.Windows.Interactivity.dll
) I know the subfolder and filename when an Add-On is being loaded, so I simply use Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location))
and Path.Combine()
to build a path to the .dll and then load it via Assembly.LoadFile()
before using reflection with assembly.GetExportedTypes()
to find the class that inherits for my 'EntryPointBase' and then create it with Activator.CreateInstance()
.
However, as soon as I have any references within my Add-On, an System.IO.FileNotFoundException
targeting the reference will pop up at assembly.GetExportedTypes()
I built a method to load all referenced assemblies, even made it recursive to load all references from the references, like this:
public void LoadReferences(Assembly assembly)
{
var loadedReferences = AppDomain.CurrentDomain.GetAssemblies();
foreach (AssemblyName reference in assembly.GetReferencedAssemblies())
{
//only load when the reference has not already been loaded
if (loadedReferences.FirstOrDefault(a => a.FullName == reference.FullName) == null)
{
//search in all subfolders
foreach (var location in Directory.GetDirectories(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)))
{
//GetDirectoriesRecusrive searchs all subfolders and their subfolders recursive and
//returns a list of paths for all files found
foreach (var dir in GetDirectoriesRecusrive(location))
{
var assemblyPath = Directory.GetFiles(dir, "*.dll").FirstOrDefault(f => Path.GetFileName(f) == reference.Name+".dll");
if (assemblyPath != null)
{
Assembly.LoadFile(assemblyPath);
break; //as soon as you find a vald .dll, stop the search for this reference.
}
}
}
}
}
}
and made sure all references are loaded in by checking AppDomain.CurrentDomain.GetAssemblies()
, but the exception stays the same.
It works if either all assemblies are directly in the application folder, or if all references fro the addon are already referenced by the startup application itself. Both ways are not suitable for my case, because higher ups demand on this file system and Add-Ons with new references should be able to load without touching the appication itself.
How can I load assemblies from one subfolder and their references from another without a System.IO.FileNotFoundException
?
Additional information:
<TargetFrameworks>netcoreapp3.1;net472</TargetFrameworks>
, although support for net472 should be ceased soon (currently still debugging in net472)TL;DR;
You are looking for AssemblyResolve
event of the AppDomain
. If you are loading all the plugin assemblies in the current app domain, then you need to handle the event for AppDomain.CurrentDomain
and load the requested assembly in the event handler.
No matter what folder structure you have for references, what you should do is:
AssemblyResolve
of AppDomain.CurrentDomain
and check if the requested assembly name is available files of reference folder, then load and return assembly.In this PoC I load all implementations of IPlugin
dynamically at run-time from assemblies in Plugins
folder and after loading them and resolving all dependencies at run-time, I call SayHello
method of plugins.
The application which loads plugins, doesn't have any dependency to plugins and just loads them at run-time from the following folder structure:
This is what I did for loading, resolving and calling the plugins:
var plugins = new List<IPlugin>();
var pluginsPath = Path.Combine(Application.StartupPath, "Plugins");
var referencesPath = Path.Combine(Application.StartupPath, "References");
var pluginFiles = Directory.GetFiles(pluginsPath, "*.dll",
SearchOption.AllDirectories);
var referenceFiles = Directory.GetFiles(referencesPath, "*.dll",
SearchOption.AllDirectories);
AppDomain.CurrentDomain.AssemblyResolve += (obj, arg) =>
{
var name = $"{new AssemblyName(arg.Name).Name}.dll";
var assemblyFile = referenceFiles.Where(x => x.EndsWith(name))
.FirstOrDefault();
if (assemblyFile != null)
return Assembly.LoadFrom(assemblyFile);
throw new Exception($"'{name}' Not found");
};
foreach (var pluginFile in pluginFiles)
{
var pluginAssembly = Assembly.LoadFrom(pluginFile);
var pluginTypes = pluginAssembly.GetTypes()
.Where(x => typeof(IPlugin).IsAssignableFrom(x));
foreach (var pluginType in pluginTypes)
{
var plugin = (IPlugin)Activator.CreateInstance(pluginType);
var button = new Button() { Text = plugin.GetType().Name };
button.Click += (obj, arg) => MessageBox.Show(plugin.SayHello());
flowLayoutPanel1.Controls.Add(button);
}
}
And this is the result:
You can download or clone the code:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With