.net – How to detect that there are no appdomain proxy objects left

appdomainnet

I have a system which loads assemblies into an appdomain and then hands out object proxies to other domains (basically a simple IoC container). We need to achieve very high uptime, and being able to update implementations at runtime helps this a lot.

I've go no problems with this in that I can fire up a new appdomain, load new assemblies into that and start handing out proxies to the nice new objects while currently executing code contines with the old objects. All is well, except that at some point I would like to unload the old appdomain.

Is there a way to check if all the proxies pointing to objects in a particular appdomain have gone out of scope so I can safely call unload without killing any long running processes?

Best Answer

If the proxy is bi-directional, it would be easiest if the provider appdomain called a method on the proxy to determine if it was still alive/active. Lifetime management over appdomain boundaries isn't a simple problem to solve, the primary problem is that you can't guarantee that consumer appdomains will function correctly (e.g. Ref Counting isn't really a good route to take, though the most obvious.)

However, if all proxies live within the same Process you can in fact rely on the GC performing proper finalization, thus, you could implement proxies such that a Finalize() on a proxy instance "phones home" to the provider appdomain to let the provider know that the proxy is no longer alive.