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开发者_StackOverflow社区 down and otherwise control its behavior.
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?
Is there another way to achieve the same thing without spawning OS processes?
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(). 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.
You should create eventHandler for DomainUnload event so when you unload your child domain you shold check if it has any childs and then unload them.
精彩评论