[英]How can MEF AssemblyCatalogs be used with custom AssemblyLoadContexts
我正在研究可通過插件擴展的 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);
這有效,我可以正常使用程序集。 但是, AssemblyCatalog都具有未填充的部件屬性,因此不會組合。
現在,如果我將程序集加載到默認上下文( AssemblyLoadContext.Default
)中,一切都會按預期工作,當然除了卸載。
我錯過了什么? 感謝您為我指明正確的方向:)
好吧,事實證明我犯了一個錯誤。 我的插件項目將CopyLocalLockFileAssemblies
設置為true
,因此,將 NuGet 依賴項復制到 output 目錄。 由於我將 output 復制到 API 加載其插件的目錄中,因此 API 也嘗試加載這些引用的庫。 現在,API 和插件項目參考System.ComponentModel.Composition
和 - 長話短說 - DLL 在不同版本中加載了兩次。 API 使用的是6.0.0
而插件使用的是4.0.0
。
因此,屬性比較(MEF 通過查找ExportAttribute
找到其導出)稍后會失敗,這就是AssemblyCatalog
無法找到導出的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.