简体   繁体   English

MEF AssemblyCatalogs 如何与自定义 AssemblyLoadContexts 一起使用

[英]How can MEF AssemblyCatalogs be used with custom AssemblyLoadContexts

I am working on an ASP.NET Core API that is extensible with Plug-Ins.我正在研究可通过插件扩展的 ASP.NET 核心 API。 Because the Plug-In Assemblies need to be unloadable, I've started loading them into separate, collectible, AssemblyLoadContexts .因为插件程序集需要是可卸载的,所以我已经开始将它们加载到单独的、可收集的AssemblyLoadContexts中。

The Implementation basically looks like this:实现基本上是这样的:

// Creating a new Context for the Plugin.
AssemblyLoadContext context = new AssemblyLoadContext(directoryInfo.FullName, true);
...

// Loading every Assembly the Plugin uses into the Context.
Assembly assembly = context.LoadFromAssemblyPath(fileInfo.FullName);
...

// Creating AssemblyCatalogs with the Assemblies.
AssemblyCatalog assemblyCatalog = new AssemblyCatalog(assembly);

This works and I can use the Assemblies normally.这有效,我可以正常使用程序集。 However, the AssemblyCatalogs all have unpopulated Parts Properties and thus will not compose.但是, AssemblyCatalog都具有未填充的部件属性,因此不会组合。

Now, if I load the Assemblies into the Default Context ( AssemblyLoadContext.Default ), everything works as expected, except the unloading of course.现在,如果我将程序集加载到默认上下文( AssemblyLoadContext.Default )中,一切都会按预期工作,当然除了卸载。

What am I missing?我错过了什么? Thanks for pointing me in the right direction:)感谢您为我指明正确的方向:)

Okay, turns out I've made a mistake.好吧,事实证明我犯了一个错误。 My Plug-In Project had CopyLocalLockFileAssemblies set to true and because of this, copied the NuGet dependencies to the output directory.我的插件项目将CopyLocalLockFileAssemblies设置为true ,因此,将 NuGet 依赖项复制到 output 目录。 Since I would then just copy the output to the directory the API loads its Plug-Ins from, the API tried to load these referenced libraries as well.由于我将 output 复制到 API 加载其插件的目录中,因此 API 也尝试加载这些引用的库。 Now, both the API and Plug-In Project reference System.ComponentModel.Composition and - long story short - the DLL was loaded twice, in different versions.现在,API 和插件项目参考System.ComponentModel.Composition和 - 长话短说 - DLL 在不同版本中加载了两次。 The API was using 6.0.0 and the Plug-In was using 4.0.0 . API 使用的是6.0.0而插件使用的是4.0.0

Because of this, the attribute comparison (MEF finds its exports by looking for the ExportAttribute ) would later fail which is why the AssemblyCatalog was not able to find the Exports.因此,属性比较(MEF 通过查找ExportAttribute找到其导出)稍后会失败,这就是AssemblyCatalog无法找到导出的原因。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM