简体   繁体   English

关闭一个AppDomain及其创建的所有AppDomain

[英]Shutdown an AppDomain and all the AppDomains it created

I want to run a third party library (XSP in this instance) inside an app domain so I can shut it down and otherwise control its behavior. 我想在应用程序域内运行第三方库(在这种情况下为XSP),以便我可以将其关闭并控制其行为。

The basic process of: 基本过程:

var child = AppDomain.CreateDomain(...) 
...
AppDomain.Unload(child)

Doesn't work when the child app domain creates an app domain itself. 子应用程序域自己创建应用程序域时不起作用。 When I shut down 'child', any app domains it has created still persist. 当我关闭“子级”时,它创建的所有应用程序域仍然存在。

I'd like to be able to say 'unload this and everything inside it' or detect when an app domain creates a new app domain so I can add it to a list of things to clean up. 我希望能够说“卸载此文件及其中的所有内容”,或者检测应用程序域何时创建新的应用程序域,以便将其添加到要清除的事物列表中。

Are there APIs to support either of these approaches? 是否有API支持这两种方法?

Is there another way to achieve the same thing without spawning OS processes? 是否有另一种方法可以在不产生OS进程的情况下实现同一目标?

Haven't tried this, but if you're at least at 2.0, you should be able to configure a replacement AppDomainManager , override its CreateDomain() method, and keep track of parent-child relationships between AppDomains through the value of the static AppDomain.CurrentDomain property in the call to CreateDomain(). 尚未尝试过,但是如果您至少是2.0,则应该能够配置替换的AppDomainManager ,覆盖其CreateDomain()方法,并通过静态AppDomain的值来跟踪AppDomain之间的父子关系。 CreateDomain()调用中的.CurrentDomain属性。 Once you have track of parents and children, you can force their unloading . 一旦了解了父母和孩子,就可以强迫他们卸载

However, that's rather rude and could make your process unstable, depending on what your third party library is up to. 但是,这很不礼貌,并且可能会使您的过程不稳定,具体取决于您的第三方库在做什么。 Processes are a more expensive but also more reliable way of wrapping third party code. 流程是包装第三方代码的更昂贵但也更可靠的方法。

您应该为DomainUnload事件创建eventHandler,以便在卸载子域时,先检查它是否有任何子域,然后再卸载它们。

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

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