C# 什么是应用域?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/574708/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:50:23  来源:igfitidea点击:

What is AppDomain?

c#.netappdomain

提问by Praveen Sharma

What is an AppDomain? What are the benefits of AppDomains or why Microsoft brought the concept of AppDomains, what was the problem without AppDomains?

什么是AppDomain?AppDomains 有什么好处或者为什么微软带来了 AppDomains 的概念,没有 AppDomains 有什么问题?

Please elaborate.

请详细说明。

采纳答案by Marc Gravell

An AppDomainprovides a layer of isolation within a process. Everything you usually think of as "per program" (static variables etc) is actually per-AppDomain. This is useful for:

AnAppDomain在进程内提供了一层隔离。您通常认为是“每个程序”(静态变量等)的所有内容实际上都是每个 AppDomain 的。这对于:

  • plugins (you can unload an AppDomain, but not an assembly withinan AppDomain)
  • security (you can run a set of code with specific trust levels)
  • isolation (you can run different versions of assemblies etc)
  • 插件(您可以卸载AppDomain,但不能卸载的程序集AppDomain
  • 安全性(您可以运行一组具有特定信任级别的代码)
  • 隔离(您可以运行不同版本的程序集等)

The pain is you need to use remoting etc.

痛苦的是你需要使用远程处理等。

See MSDNfor lots more info. To be honest, it isn't something you need to mess with very often.

有关更多信息,请参阅 MSDN。老实说,这不是你需要经常弄乱的东西。

回答by Brian Rasmussen

AppDomains can be viewed as lightweight processes. They share many of the same characteristics of a process, e.g. they have their own copies of statics, assemblies and so forth, but they are contained within a single process. From the operating system's point of view a process is just a process no matter how many AppDomains it may contain.

AppDomains 可以被视为轻量级进程。它们共享一个流程的许多相同特征,例如,它们拥有自己的静态、程序集等副本,但它们包含在单个流程中。从操作系统的角度来看,一个进程只是一个进程,无论它可能包含多少个 AppDomain。

Unlike a process however, an AppDomain does not have any threads unless you explicitly create them. A thread can run code in any AppDomain.

然而,与进程不同的是,除非您明确创建线程,否则 AppDomain 没有任何线程。一个线程可以在任何 AppDomain 中运行代码。

AppDomains are part of the same process and thus actually share the same managed heap. This is usually not an issue since the AppDomain programming model prevents implicit access between AppDomains. However, some references are actually shared between AppDomains such as type objects and interned strings.

AppDomains 是同一进程的一部分,因此实际上共享相同的托管堆。这通常不是问题,因为 AppDomain 编程模型会阻止 AppDomain 之间的隐式访问。但是,一些引用实际上在 AppDomain 之间共享,例如类型对象和内部字符串。

回答by George

An App-domain implements the concept of a contiguous virtual memory space that holds the code and the in-memory resources that can be directly accesses or referenced.

应用程序域实现了一个连续的虚拟内存空间的概念,其中包含可以直接访问或引用的代码和内存资源。

Separate AppDomains do not share memory space and, consequently, one AppDomain cannot directly reference contents in another. In particular, data must be passed between AppDomains through a copy-by-value process. In particular, reference objects, which rely on pointers and therefore memory addresses, must first be serialized from the source and then deserialization into the destination AppDomain.

单独的 AppDomain 不共享内存空间,因此一个 AppDomain 不能直接引用另一个 AppDomain 中的内容。特别是,数据必须通过按值复制的过程在 AppDomain 之间传递。特别是,依赖于指针和内存地址的引用对象必须首先从源序列化,然后反序列化到目标 AppDomain。

Previously on Windows systems, memory boundaries were implemented by processes; however, constructing processes is resource intensive. They also serve a dual purpose as thread boundaries. App-domains, on the other hand, are concerned only with memory boundary or address space. Threads can 'flow' across AppDomains (that is, a procedure can invoke an entry point in another AppDomain and wait for it to return. The thread is said to 'continue' execution within the other AppDomain).

以前在 Windows 系统上,内存边界是由进程实现的;然而,构建过程是资源密集型的。它们还用作线程边界的双重目的。另一方面,应用程序域只关心内存边界或地址空间。线程可以在 AppDomain 之间“流动”(即,一个过程可以调用另一个 AppDomain 中的入口点并等待它返回。该线程被称为在另一个 AppDomain 中“继续”执行)。

One significant benefit of this architecture is that communication patterns between App-domains remain substantially unchanged whether the AppDomains are in the same process, different processes, or on a different machines all together: namely the process of serialization and deserialization (marshaling) of parameter data.

这种架构的一个显着好处是,无论 AppDomain 是在同一个进程中、不同进程中还是在不同的机器上,App 域之间的通信模式基本上保持不变:即参数数据的序列化和反序列化(编组)过程.

Note 1: the meaning of a thread crossing an AppDomain is that of a blocking or synchronous method call into another AppDomain (versus a non-blocking or asynchronous call which would spawn another thread to continue execution in the target AppDomain and continue in it's current AppDomain without awaiting response).

注 1:线程跨越 AppDomain 的含义是阻塞或同步方法调用到另一个 AppDomain(而不是非阻塞或异步调用,它会产生另一个线程以继续在目标 AppDomain 中执行并在其当前 AppDomain 中继续执行)无需等待回复)。

Note 2: there is such a thing as Thread Local Storage. However, a better name would have been App-Domain Thread Local Storage since threads leave their data behind as they cross App-Domains but pick them back up when they return: http://msdn.microsoft.com/en-us/library/6sby1byh.aspx

注2:有线程本地存储这样的东西。但是,更好的名称应该是应用程序域线程本地存储,因为线程在跨应用程序域时会将其数据留在后面,但在返回时将其取回:http: //msdn.microsoft.com/en-us/library /6sby1byh.aspx

Note3: A .Net Runtime is a Windows Process application with an associated heap. It may host one or more AppDomains in that heap. However, the AppDomains are design to be oblivious of each other and to communicate with each other via marshaling. It is conceivable that an optimization can be performed that bypasses marshaling between communicating AppDomains sharing the same .Net Runtime and therefore the same Windows Process heap.

注 3:.Net 运行时是具有关联堆的 Windows 进程应用程序。它可能在该堆中托管一个或多个 AppDomain。但是,AppDomains 被设计为相互忽略并通过编组相互通信。可以想象,可以执行优化,绕过共享相同 .Net 运行时并因此共享相同 Windows 进程堆的通信 AppDomain 之间的封送处理。