tags:

views:

23

answers:

2

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.

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?

+1  A: 

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.

Pontus Gagge
Thanks. Found some more info on this approach here http://blogs.msdn.com/b/shawnfa/archive/2004/11/12/256550.aspx
Phil
A: 

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.

Andriy Shvay
Yes, but how to find the children?
Phil
You must track your domains yourself. For example - create a dictionary Dictionary<parentDomain, List<childDomain> or any other way and process it in DomainUnload event
Andriy Shvay
Sure, but how do I find out when a child is creating a new app domain?
Phil