AppDomain 是否等同于 .NET 代码的进程? [英] Is AppDomain equivalent to a Process for .NET code?

查看:27
本文介绍了AppDomain 是否等同于 .NET 代码的进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须调用一些编写不当的第 3 方 COM 组件,这些组件存在内存泄漏并在长时间运行的进程中使用单线程单元 [STA].

I have to call some badly written 3rd party COM components that have memory leaks and uses Single Threaded Apartment [STA] within a long running process.

我知道单独的进程将是实现它的好方法,我可以偶尔从长时间运行的进程中重新启动它.

I know separate process will be nice way to implement it and I can restart it occasionally from the long running process.

可以改用 AppDomain 吗?如果标记适当,AppDomain 线程是否为 STA 线程?它有自己的 COM 对象内存吗?卸载AppDomain就相当于杀了进程?

Can AppDomain be used instead? Is AppDomain thread a STA thread if marked appropiately? Does it have its own memory for COM objects? Is unloading the AppDomain is equivalent of killing the process?

推荐答案

AppDomain 提供的隔离程度与进程不同.事实上,如果您担心第 3 方组件状态不佳,则存在风险,它会关闭您的 .NET 应用程序.

An AppDomain does not provide the same degree of isolation as a process does. In fact if you're worried that the 3rd party component is not in good shape there's a risk, that it will take down your .NET application.

如果卸载时正在执行非托管代码,则无法卸载 AppDomain,因此您可能难以控制 AppDomain 中的第 3 方代码.请参阅 http://msdn.microsoft.com/en-us/library/system.appdomain.unload.aspx

An AppDomain cannot be unloaded if unmanaged code is executing at the time of unload, so you may have a hard time controlling your 3rd party code in an AppDomain. See http://msdn.microsoft.com/en-us/library/system.appdomain.unload.aspx

即使仅针对托管代码,AppDomain 也不提供强大的沙盒解决方案.例如.如果加载的代码产生任何线程,这些将在未处理的异常的情况下关闭整个进程.这个问题有更多信息:.NET - 实现捕获所有异常处理程序"的最佳方法是什么.

Even for managed code only, an AppDomain does not provide a robust sandbox solution. E.g. if the loaded code spawns any threads these will take down the entire process in case of unhandled exceptions. This question has a bit more info: .NET - What's the best way to implement a "catch all exceptions handler".

据我所知,在 .NET 应用程序中托管此类代码的最佳选择是实现您自己的 CLR 主机进程,如 IIS 和 SQL Server.

As far as I am aware the best option for hosting code like that in a .NET application is to implement your own CLR host process like IIS and SQL Server does.

这篇关于AppDomain 是否等同于 .NET 代码的进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆