问题描述
遵循本讲座(https://docs.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support),我设法创建了一个基于插件的应用程序.到现在为止还挺好.但是,我想用我的插件做更多事情.因此,对于其中一个,我创建了一个额外的库.当然,在开发我的插件时,我对我创建的这个库有一个库依赖.问题更多在于执行.在运行我的应用程序时,尤其是当插件执行其任务时,我提出了一个异常说:
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 当然是被调用的插件使用的附加库.有什么我错过了吗?
MyLibObjectsLib is of course the additional library used by the called plugin.Is there something I missed?
推荐答案
本教程使用自定义 ALC (AssemblyLoadContext) 仅加载一个不依赖其他插件程序集.
The tutorial use a custom ALC (AssemblyLoadContext) to load only one plugin assembly which doesn't depend on others.
在您的情况下,自定义 ALC 加载插件并解决所有依赖项.
In your case, the custom ALC load the plugin and resolve all dependencies.
在 自定义 ALC,您注意到它使用 AssemblyDependencyResolver(.Net Core 3.0+ 中的一个新类)
In the custom ALC, you note that it uses AssemblyDependencyResolver ( a new class in .Net Core 3.0+)
AssemblyDependencyResolver 类使应用程序开发人员能够更轻松地开发插件架构,并结合自定义 System.Runtime.Loader.AssemblyLoadContext 实例来隔离插件,并使插件能够加载依赖项.
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.
重要提示:
插件应包括 ExcludeAssets=runtime
用于项目引用共享程序集,参考.
Plugins should include ExcludeAssets=runtime
for project references to shared assemblies, ref.
例如,如果项目Common
包含接口`IContract'并且被宿主应用程序和插件项目引用,则添加如下引用:
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="..IContractCommon.csproj">
<Private>false</Private>
<ExcludeAssets>runtime</ExcludeAssets>
</ProjectReference>
<ProjectReference Include="..MyLibObjectsLibMyLibObjectsLib.csproj" />
</ItemGroup>
此代码用于加载插件:
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();
}
我将您的插件模拟为标准 2.0 库,插件参考了 OLEDB nuget 包,并且插件加载没有错误.
I simulated your plugin as standard2.0 library and the plugin reference OLEDB nuget package and the plugin is loaded without errors.
这篇关于.Net 核心和插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!