在聚合目录中使用后无法删除目录
Unable to Delete Directory after using in in an Aggregate Catalog
我在 Visual Studio 2017 年使用 MEF (System.ComponentModel.Composition) CompositionContainer,使用 .net 4.5.1 从目录 X 的 dll 加载一些 类。
_catalogs = new AggregateCatalog();
var dir = new DirectoryCatalog("X", "*.dll");
compositionContainer = new CompositionContainer(_catalogs, true);
compositionContainer.ComposeParts();
我可以毫无问题地访问我需要的内容。
在某些时候,我正在从组合中删除目录:
_catalogs.Catalogs.Remove(_catalogs.Catalogs.First());
这有效,之后,我在 CompositionContainer 或 AggregateCatalog 中没有我的 dll 实例。
我的问题是现在,如果我的应用程序仍然打开,我不能删除目录 "X",也不能移动它或重命名它。我从代码或资源管理器收到 'Access to path is denied' 消息。
任何人都知道什么会导致目录即使在删除后仍然附加到 CompositionContainer?还是我删错了?
谢谢。
Pfff...仅删除目录甚至调用 Dispose()
是不够的。为什么?因为默认情况下 MEF 将目录的程序集加载到 current AppDomain。这意味着您的程序集文件被您的 AppDomain 阻止,这就是为什么您不能对它们执行任何 IO 操作的原因。
关于装卸程序集来自documentation:
There is no way to unload an individual assembly without unloading all of the application domains that contain it. Use the Unload method from AppDomain to unload the application domains. For more information, see How to: Unload an Application Domain.
现在,显然您不能卸载您的 AppDomain,因为您只有一个 =)。所以,让我们只有一个选择,是的,你是对的:使用另一个 AppDomain
几个月前我有完全相同的要求,我遵循了 this CodeProject 文章,这对我帮助很大。文章的名称是 MEF 和 AppDomain - 即时删除程序集,这正是您所需要的 =)
编辑
OP 通过默认的 appDomain 设置 ShadowCopyFiles on:
解决了这个问题
AppDomain.CurrentDomain.SetShadowCopyFiles();
我在 Visual Studio 2017 年使用 MEF (System.ComponentModel.Composition) CompositionContainer,使用 .net 4.5.1 从目录 X 的 dll 加载一些 类。
_catalogs = new AggregateCatalog();
var dir = new DirectoryCatalog("X", "*.dll");
compositionContainer = new CompositionContainer(_catalogs, true);
compositionContainer.ComposeParts();
我可以毫无问题地访问我需要的内容。 在某些时候,我正在从组合中删除目录:
_catalogs.Catalogs.Remove(_catalogs.Catalogs.First());
这有效,之后,我在 CompositionContainer 或 AggregateCatalog 中没有我的 dll 实例。
我的问题是现在,如果我的应用程序仍然打开,我不能删除目录 "X",也不能移动它或重命名它。我从代码或资源管理器收到 'Access to path is denied' 消息。
任何人都知道什么会导致目录即使在删除后仍然附加到 CompositionContainer?还是我删错了?
谢谢。
Pfff...仅删除目录甚至调用 Dispose()
是不够的。为什么?因为默认情况下 MEF 将目录的程序集加载到 current AppDomain。这意味着您的程序集文件被您的 AppDomain 阻止,这就是为什么您不能对它们执行任何 IO 操作的原因。
关于装卸程序集来自documentation:
There is no way to unload an individual assembly without unloading all of the application domains that contain it. Use the Unload method from AppDomain to unload the application domains. For more information, see How to: Unload an Application Domain.
现在,显然您不能卸载您的 AppDomain,因为您只有一个 =)。所以,让我们只有一个选择,是的,你是对的:使用另一个 AppDomain
几个月前我有完全相同的要求,我遵循了 this CodeProject 文章,这对我帮助很大。文章的名称是 MEF 和 AppDomain - 即时删除程序集,这正是您所需要的 =)
编辑 OP 通过默认的 appDomain 设置 ShadowCopyFiles on:
解决了这个问题AppDomain.CurrentDomain.SetShadowCopyFiles();