.Net核心和插件 [英] .Net core and plugins

查看:57
本文介绍了.Net核心和插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在本讲座之后( https://docs.microsoft.com/zh-cn/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.

例如,如果包含接口"IContract"的项目" Common "同时被主机应用程序和插件项目引用,则添加如下引用:

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>

此代码用于加载插件:

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();     
}

我将您的插件模拟为standard2.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核心和插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆