weikio / PluginFramework

Everything is a Plugin in .NET
MIT License
549 stars 105 forks source link

Resolve additional (shared) DLL paths automatically #23

Closed mikoskinen closed 4 years ago

mikoskinen commented 4 years ago

Issue:

The future version should automatically try to resolve the additional runtime/shared paths/frameworks required by the plugins:

image

Relates to #21 where we had to manually add additional runtime path:

PluginLoadContextOptions.Defaults.AdditionalRuntimePaths.Add(Path.GetDirectoryName(typeof(Button).Assembly.Location));

The AspNetCore-library also has this:

        var aspNetCoreLocation = Path.GetDirectoryName(aspNetCoreControllerAssemblyLocation);
        PluginLoadContextOptions.Defaults.AdditionalRuntimePaths.Add(aspNetCoreLocation);

Without these, the plugins can't load all the required DLLs.

Possible solution:

It might be possible to use MetadataReference to get the references. For example:

var assemblyRef = MetadataReference.CreateFromFile(assemblyPath);
var references = assembly.GetReferencedAssemblies();

This must be done recursively to find all the referenced assemblies and their locations. This info is then added to PathAssemblyResolver in FolderPluginCatalog.

mikoskinen commented 4 years ago

The pull request takes care of adding the shared frameworks used by host application into to the assembly resolver.

This should remove the need of having to define AdditionalRuntimePaths in many situations. If your host app is WinForms and your plugin uses WinForm dlls, AdditionalRuntimePaths isn't needed any more.

But, in situations where the plugin is using shared framework (Desktop / ASP.NET Core) and your host application isn't, AdditionalRuntimePaths is still needed. This can happen for example if your Console App points to a folder where DLL's require references to Microsoft.WindowsDesktop.App (C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\3.1.5).