更改使用 MEF 加载的 Dll
我正在使用 MEF 和 System.ComponentModel.Composition.dll 来加载一些 dll。
我正在做类似的事情:
AggregateCatalog catalog = new AggregateCatalog(new AssemblyCatalog(Assembly.GetExecutingAssembly()), new DirectoryCatalog(directory));
_container = new CompositionContainer(catalog);
_container.ComposeParts(this);
导入我的 dll。
一段时间后,我想更新我的 dll,但如果我尝试删除它,我会被拒绝访问,因为它已被程序使用。
如何释放该 dll、替换为新的 dll 并再次加载该 dll? (无需关闭程序)
提前感谢您的帮助
I'm using MEF and the System.ComponentModel.Composition.dll to load some dll.
I'm doing something like :
AggregateCatalog catalog = new AggregateCatalog(new AssemblyCatalog(Assembly.GetExecutingAssembly()), new DirectoryCatalog(directory));
_container = new CompositionContainer(catalog);
_container.ComposeParts(this);
to import my dll.
After some times, I would like to update my dll but if I try to delete it, I have an access denied, because it's alrealdy used by the program.
How can I release the dll, replace with a new dll and load the dll again ? (without closing the program)
Thanks in advance for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在 AppDomain 中启用卷影复制,这会强制应用程序的行为类似于 Web 应用程序,其中可执行内容不是从源位置运行,而是从临时位置运行。
您遇到的唯一问题是
AppDomain.CurrentDomain.SetShadowCopyFiles()
,该方法将其强制应用于当前域。不建议这样做,因为这已被弃用,取而代之的是:AppDomainSetup.ShadowCopyFiles = "true";
。然后,您需要推迟在其他 AppDomain 中执行程序集。也许这个表单帖子可以提供帮助?我不确定您是否可以通过应用程序配置启用卷影复制...
You need to enable shadow copying in the AppDomain, this forces the application to behave similarly to a web app where the executable content is not running from the source location, but a temporary location.
The only problem you have is either
AppDomain.CurrentDomain.SetShadowCopyFiles()
which forces it on the the current domain. Not advisable as this has been deprecated in favour of:AppDomainSetup.ShadowCopyFiles = "true";
when creating a new AppDomain. You then need to defer execution of the your assembly in the other AppDomain. Maybe this form post can help?I'm not sure you can enable Shadow Copying through application configuration...
如果您尝试在目录中插入一个对象组件,如下所示:
您可以稍后删除\更改文件...
If you try to insert on catalog one Object Assembly like this:
You can Delete\Change the file later...