如何检测没有留下任何appdomain代理对象
我有一个系统,它将程序集加载到应用程序域中,然后将对象代理分发给其他域(基本上是一个简单的 IoC 容器)。我们需要实现非常高的正常运行时间,并且能够在运行时更新实现对此有很大帮助。
我对此没有任何问题,因为我可以启动一个新的应用程序域,将新程序集加载到其中,并开始向漂亮的新对象分发代理,同时当前执行的代码继续使用旧对象。一切都很好,除了在某些时候我想卸载旧的应用程序域。
有没有办法检查指向特定应用程序域中的对象的所有代理是否已超出范围,以便我可以安全地调用卸载而不终止任何长时间运行的进程?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果代理是双向的,则最简单的方法是提供者应用程序域调用代理上的方法来确定它是否仍然存在/活动。对应用程序域边界的生命周期管理并不是一个简单的问题,主要问题是您无法保证消费者应用程序域能够正常运行(例如,引用计数并不是一个很好的方法,尽管是最明显的。)
但是,如果所有代理都位于同一个进程中,您实际上可以依赖 GC 执行正确的终结,因此,您可以实现代理,以便代理实例上的 Finalize() “打电话回家”到提供程序应用程序域,让提供程序知道代理不再存在。
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.