什么是 .NET 应用程序域?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1094478/
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
What is a .NET application domain?
提问by Luke
In particular, what are the implications of running code in two different application domains?
特别是,在两个不同的应用程序域中运行代码的含义是什么?
How is data normally passed across the application domain boundary? Is it the same as passing data across the process boundary? I'm curious to know more about this abstraction and what it is useful for.
数据通常如何跨应用程序域边界传递?它与跨进程边界传递数据相同吗?我很想知道更多关于这个抽象及其用途的信息。
EDIT:Good existing coverage of the AppDomain class in general at I don't understand Application Domains
编辑:AppDomain 类的现有覆盖一般在我不了解应用程序域
采纳答案by Reed Copsey
An AppDomainbasically provides an isolated region in which code runs inside of a process.
一个应用程序域基本上提供其中内部代码的过程的运行的分离的区域。
An easy way to think of it is almost like a lighter-weight process sitting inside of your main process. Each AppDomain exists within a process in complete isolation, which allows you to run code safely (it can be unloaded without tearing down the whole process if needed), with separate security, etc.
一种简单的方式来思考它几乎就像一个位于主进程中的轻量级进程。每个 AppDomain 都完全隔离地存在于一个进程中,这使您可以安全地运行代码(如果需要,可以在不拆除整个进程的情况下卸载它),具有单独的安全性等。
As to your specifics - if you run code in 2 different AppDomains within a process, the code will run in isolation. Any communication between the AppDomains will get either serialized or handled via MarshallByRefObject. It behaves very much like using remoting in this regard. This provides a huge amount of security - you can run code that you don't trust, and if it does something wrong, it will not affect you.
至于您的具体情况 - 如果您在一个进程内的 2 个不同 AppDomain 中运行代码,则代码将单独运行。AppDomains 之间的任何通信都将通过 MarshallByRefObject 进行序列化或处理。在这方面,它的行为非常类似于使用远程处理。这提供了大量的安全性 - 您可以运行您不信任的代码,如果它做错了什么,它不会影响您。
There are many more details in MSDN's description of Application Domains.
MSDN 对应用程序域的描述中有更多详细信息。
回答by nikmd23
It is an isolation layer provided by the .NET runtime. As such, App domains live with in a process (1 process can have many app domains) and have their own virtual address space.
它是 .NET 运行时提供的隔离层。因此,应用程序域存在于一个进程中(一个进程可以有多个应用程序域)并拥有自己的虚拟地址空间。
App domains are useful because:
应用程序域很有用,因为:
- They are less expensive than full processes
- They are multithreaded
- You can stop one without killing everything in the process
- Segregation of resources/config/etc
- Each app domain runs on its own security level
- 它们比全流程更便宜
- 它们是多线程的
- 您可以在不杀死过程中的所有内容的情况下停止一个
- 资源/配置/等的隔离
- 每个应用程序域都在自己的安全级别上运行
回答by Ziaullah Khan
If you look at it from processor internal details perspective it sets different value for Code Segment (the CS) register. code and CS:IP (Instruction Pointer) register is the one that is in execution by the processor.
如果从处理器内部细节的角度来看它,它会为代码段(CS)寄存器设置不同的值。代码和 CS:IP(指令指针)寄存器是由处理器执行的寄存器。
(I've chosen to skim page table related discussion for brevity).
(为简洁起见,我选择略读与页表相关的讨论)。
AppDomain marks this boundary. for code safety.
AppDomain 标志着这个边界。为了代码安全。
The reason for giving this background is to get away with question of these sort: 1. how can we access resource across two app domain (yes using pipes or some other sharing mechanis not directly as CS:IP cant be set to some other appdomain. It is just the OS that can do it. Not the CLR)
给出这个背景的原因是为了摆脱这些问题:1.我们如何跨两个应用程序域访问资源(是的,使用管道或其他一些共享机制不是直接的,因为 CS:IP 不能设置为其他一些应用程序域。只有操作系统可以做到这一点。而不是 CLR)
Could there be multiple threads in app domain. Technically yes as the CS value going to be in the current process. you can change IP to something else by a jump statement (function call/goto combination)
can two threads in two different app domain communicate (No. refer point 1.)
can two threads in single app domain communicate (Yes. refer point 2)
应用程序域中是否有多个线程。从技术上讲是的,因为 CS 值将在当前进程中。您可以通过跳转语句(函数调用/转到组合)将 IP 更改为其他内容
两个不同应用程序域中的两个线程是否可以通信(否。参考点 1。)
单个应用程序域中的两个线程可以通信吗(是的。请参阅第 2 点)
several other combination of these cases could be answered by little knowledge of how CS:IP works.
对 CS:IP 的工作原理知之甚少,可以回答这些情况的其他几种组合。
回答by Afazal
Each application running within a process, AppDomain is also a light-weight process or we can say logical unit which having group of assemblies (this is a container which contain group of assemblies ) and this exist inside of the process at isolation level of same process, this allows to run multiple assemblies within same process and prevent them for direct access.
每个应用程序在一个进程中运行,AppDomain 也是一个轻量级进程,或者我们可以说逻辑单元具有一组程序集(这是一个包含一组程序集的容器)并且它存在于同一进程的隔离级别的进程内部,这允许在同一进程中运行多个程序集并防止它们直接访问。
Running Dot Net Application Within AppDomain:Once any dot net application run, Operation system shell load CLR into a process and new AppDomain been created in the same process and load all the assemblies in the created AppDomain, now from the AppDomain code will get executed.
在 AppDomain 中运行 Dot Net 应用程序:一旦任何 dot net 应用程序运行,操作系统 shell 将 CLR 加载到一个进程中,并在同一进程中创建新的 AppDomain 并加载创建的 AppDomain 中的所有程序集,现在将从 AppDomain 代码执行。
When to Custom AppDomain:We can create own AppDomain, now the thing is in which scenario we can create own AppDomain. Suppose run time we need to add or remove the assemblies without interruption the running application then we can create own AppDomain.
何时自定义AppDomain:我们可以创建自己的AppDomain,现在是在什么场景下我们可以创建自己的AppDomain。假设运行时我们需要在不中断正在运行的应用程序的情况下添加或删除程序集,那么我们可以创建自己的 AppDomain。

