.net 使用 AppDomain 的好例子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/97433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Good example of use of AppDomain
提问by hughdbrown
I keep getting asked about AppDomains in interviews, and I know the basics:
我在面试中不断被问及 AppDomains,我知道以下基础知识:
- they are an isolation level within an application (making them different from applications)
- they can have threads (making them different from threads)
- exceptions in one appdomain do not affect another
- appdomains cannot access each other's memory
- each appdomain can have different security
- 它们是应用程序中的隔离级别(使它们与应用程序不同)
- 它们可以有线程(使它们与线程不同)
- 一个应用程序域中的异常不会影响另一个
- appdomains 不能访问彼此的内存
- 每个应用程序域可以有不同的安全性
I still don't get what makes them necessary. I'm looking for a reasonable concrete circumstance when you would use one.
我仍然不明白是什么让它们变得必要。我正在寻找一个合理的具体情况,当您使用它时。
Answers:
答案:
- Untrusted code
- Core application protected
Untrusted/3rd party plugins are barred from corrupting shared memory and non-authorized access to registry or hard drive by isolation in separate appdomain with security restrictions, protecting the application or server. e.g. ASP.NET and SQL Server hosting component code - Trusted code
- Stability
Application segmented into safe, independent features/functionality - Architectural flexibility
Freedom to run multiple applications within a single CLR instance or each program in its own.
- 不受信任的代码
- 受核心应用程序保护的
不受信任/第 3 方插件通过隔离在具有安全限制的单独应用程序域中而被禁止破坏共享内存和对注册表或硬盘驱动器的非授权访问,从而保护应用程序或服务器。例如 ASP.NET 和 SQL Server 托管组件代码 - 可信代码
- 稳定性
应用程序细分为安全、独立的特性/功能 - 架构灵活性
在单个 CLR 实例或每个程序中自由运行多个应用程序。
Anything else?
还要别的吗?
回答by Michael Burr
Probably the most common one is to load assemblies that contain plug-in code from untrusted parties. The code runs in its own AppDomain, isolating the application.
可能最常见的一种是加载包含来自不受信任方的插件代码的程序集。代码在其自己的 AppDomain 中运行,隔离应用程序。
Also, it's not possible to unload a particular assembly, but you can unload AppDomains.
此外,无法卸载特定程序集,但您可以卸载 AppDomains。
For the full rundown, Chris Brumme had a massive blog entry on this:
对于完整的纲要,Chris Brumme 有一个关于此的大量博客条目:
http://blogs.msdn.com/cbrumme/archive/2003/06/01/51466.aspx
http://blogs.msdn.com/cbrume/archive/2003/06/01/51466.aspx
https://devblogs.microsoft.com/cbrumme/appdomains-application-domains/
https://devblogs.microsoft.com/cbrume/appdomains-application-domains/
回答by Jon Turner
Another benefit of AppDomains (as you mentioned in your question) is code that you load into it can run with different security permissions. For example, I wrote an app that dynamically loaded DLLs. I was an instructor and these were student DLLs I was loading. I didn't want some disgruntled student to wipe out my hard drive or corrupt my registry, so I loaded the code from their DLLs into a separate AppDomain that didn't have file IO permissions or registry editing permissions or even permissions to display new windows (it actually only had execute permissions).
AppDomains 的另一个好处(正如您在问题中提到的)是您加载到其中的代码可以以不同的安全权限运行。例如,我编写了一个动态加载 DLL 的应用程序。我是一名讲师,这些是我正在加载的学生 DLL。我不想让一些心怀不满的学生清除我的硬盘驱动器或损坏我的注册表,所以我将代码从他们的 DLL 加载到一个单独的 AppDomain 中,该 AppDomain 没有文件 IO 权限或注册表编辑权限,甚至没有显示新窗口的权限(它实际上只有执行权限)。
回答by C. Dragon 76
I think the main motivation for having AppDomains is that the CLR designers wanted a way of isolating managed code without incurring the performance overhead of multiple Windows processes. Had the CLR been originally implemented on top of UNIX (where creating multiple processes is significantly less expensive), AppDomains may never have been invented.
我认为拥有 AppDomains 的主要动机是 CLR 设计者想要一种隔离托管代码的方法,而不会产生多个 Windows 进程的性能开销。如果 CLR 最初是在 UNIX 之上实现的(创建多个进程的成本要低得多),那么 AppDomains 可能永远不会被发明。
Also, while managed plug-in architectures in 3rd party apps is definitely a good use of AppDomains, the bigger reason they exist is for well-known hosts like SQL Server 2005 and ASP.NET. For example, an ASP.NET hosting provider can offer a shared hosting solution that supports multiple sites from multiple customers all on the same box running under a single Windows process.
此外,虽然 3rd 方应用程序中的托管插件架构绝对是 AppDomains 的一个很好的用途,但它们存在的更大原因是用于 SQL Server 2005 和 ASP.NET 等知名主机。例如,ASP.NET 托管提供商可以提供一个共享托管解决方案,该解决方案支持来自多个客户的多个站点,所有站点都在同一个机器上运行在单个 Windows 进程下。
回答by Mark Cidade
If you create an application that allows 3rd-party plug-ins, you can load those plug-ins in a separate AppDomain so that your main application is safe from unknown code.
如果您创建的应用程序允许使用 3rd 方插件,您可以在单独的 AppDomain 中加载这些插件,以便您的主应用程序免受未知代码的影响。
ASP.NET also uses separate AppDomains for each web application within a single worker process.
ASP.NET 还为单个工作进程中的每个 Web 应用程序使用单独的 AppDomain。
回答by FlySwat
App Domains are great for application stability.
App Domains 非常适合应用程序稳定性。
By having your application consist of a central process, which then spawns out "features" in separate appdomains, you can can prevent a global crash should one of them misbehave.
通过让您的应用程序由一个中央进程组成,然后在不同的应用程序域中产生“功能”,如果其中一个行为不端,您可以防止全局崩溃。
回答by Garth Gilmour
As I understand it AppDomain's are designed to allow the hosting entity (OS, DB, Server etc...) the freedom to run multiple applications within a single CLR instance or each program in its own. So its an issue for the host rather than the application developer.
据我了解,AppDomain 旨在允许托管实体(操作系统、数据库、服务器等)自由地在单个 CLR 实例或每个程序中运行多个应用程序。因此,这是主机而不是应用程序开发人员的问题。
This compares favourably with Java where you always have 1 JVM per application, often resulting in many instances of the JVM running side by side with duplicated resources.
与 Java 相比,每个应用程序总是有 1 个 JVM,这通常会导致 JVM 的许多实例与重复的资源并行运行。
回答by nightcoder
I see 2 or 3 main use cases for creating separate application domains:
我看到了用于创建单独应用程序域的 2 或 3 个主要用例:
1)Process-like isolation with low resource usage and overhead. For example, this is what ASP.NET does - it hosts each website in a separate application domain. If it used different threads in the single application domain then code of different websites could interfere with each other. If it hosted different websites in different processes - it would use much resources and also interprocess communication is relatively difficult comparing to inprocess communication.
1)具有低资源使用和开销的类进程隔离。例如,这就是 ASP.NET 所做的 - 它在单独的应用程序域中托管每个网站。如果它在单个应用程序域中使用不同的线程,那么不同网站的代码可能会相互干扰。如果它在不同的进程中托管不同的网站 - 它会使用很多资源,并且与进程内通信相比,进程间通信相对困难。
2)Executing untrusted code in a separate application domain with particular security permissions (this is actually related to 1st reason). As people already said, you could load 3rd party plugins or untrusted dlls into separate application domains.
2)在具有特定安全权限的单独应用程序域中执行不受信任的代码(这实际上与第一个原因有关)。正如人们已经说过的,您可以将 3rd 方插件或不受信任的 dll 加载到单独的应用程序域中。
3)Ability to unload assemblies to reduce unnecessary memory usage. Unfortunately, there is no way to unload an assembly from an application domain. So if you load some big assembly to your main application domain, the only way to free the corresponding memory after that assembly is not needed anymore is to close your application. Loading assemblies in a separate application domain and unloading that application domain when those assemblies are not needed anymore is a solution to this problem.
3)能够卸载程序集以减少不必要的内存使用。不幸的是,没有办法从应用程序域卸载程序集。因此,如果您将一些大型程序集加载到您的主应用程序域,则在不再需要该程序集之后释放相应内存的唯一方法是关闭您的应用程序。在单独的应用程序域中加载程序集并在不再需要这些程序集时卸载该应用程序域是解决此问题的方法。

