我正在开发一个ASP.NET核心API,它可以用插件进行扩展。因为插件程序集需要是可卸载的,所以我已经开始将它们加载到单独的、可收集的AssemblyLoadContexts中。
实现基本如下所示:
// 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);
这是可行的,我可以正常使用大会。但是,AssemblyCatalogs都有未填充的部件属性,因此不会组成。
现在,如果我将程序集加载到默认上下文(AssemblyLoadContext.Default
)中,除了卸载之外,一切都按预期的方式工作。
我遗漏了什么?谢谢你为我指明了正确的方向:)
发布于 2022-09-13 18:59:50
好吧,原来我犯了个错误。我的插件项目将CopyLocalLockFileAssemblies
设置为true
,因此将NuGet依赖项复制到输出目录。因为我会将输出复制到API加载插件的目录中,所以API也尝试加载这些引用库。现在,API和插件项目参考System.ComponentModel.Composition
和长话短说- DLL被加载了两次,在不同的版本。API使用的是6.0.0
,插件使用的是4.0.0
。
因此,属性比较(MEF通过查找ExportAttribute
查找其导出)稍后会失败,这就是为什么AssemblyCatalog
无法找到导出。
https://stackoverflow.com/questions/73654704
复制相似问题