.Net core and plugins

寵の児 提交于 2021-01-28 11:13:44

问题


Following this lecture (https://docs.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support), I managed to create an app based on plugins. So far so good. But, I want to do more with my plugins. Hence, for one of them, I created an additional library. Of course, when developing my plugin, I got a library dependance to this lib I created. The problem is more on execution. When running my app, more especially when the plugin executes its task, I have an exception raised saying:

Could not load file or assembly 'MyLibObjectsLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

MyLibObjectsLib is of course the additional library used by the called plugin. Is there something I missed?


回答1:


The tutorial use a custom ALC (AssemblyLoadContext) to load only one plugin assembly which doesn't depend on others.

In your case, the custom ALC load the plugin and resolve all dependencies.

In the custom ALC, you note that it uses AssemblyDependencyResolver ( a new class in .Net Core 3.0+)

The AssemblyDependencyResolver class enables the application developers to more easily develop a plugin architecture in conjunction with custom System.Runtime.Loader.AssemblyLoadContext instances to isolate plugins and also enable plugins to load dependencies.

Important Remark:

Plugins should include ExcludeAssets=runtime for project references to shared assemblies, ref.

For example, If the project Common that include the interface `IContract' and is referenced by the both host application and the plugin project, add reference as given below:

<!-- in the plugin project -->

<ItemGroup>
        <ProjectReference Include="..\IContract\Common.csproj">
            <Private>false</Private>
            <ExcludeAssets>runtime</ExcludeAssets>
        </ProjectReference>
        <ProjectReference Include="..\MyLibObjectsLib\MyLibObjectsLib.csproj" />
    </ItemGroup>

This code is used for loading the plugin:

string pluginPath = @"path/to/MyContract.Plugin.dll";           
var assembly = new PluginLoadContext(pluginPath).LoadFromAssemblyPath(pluginPath);

var type = assembly.GetTypes()
       .FirstOrDefault(t => t.IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract);     

if (type != null) {
     var instance = (IContract)Activator.CreateInstance(type);
     var ret = instance.Execute();     
}

I simulated your plugin as standard2.0 library and the plugin reference OLEDB nuget package and the plugin is loaded without errors.



来源:https://stackoverflow.com/questions/60209723/net-core-and-plugins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!