C#中AppDomain的使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/665668/
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
Usage of AppDomain in C#
提问by Nipun
What is the most important use of AppDomains in C#?
AppDomains 在 C# 中最重要的用途是什么?
采纳答案by Marc Gravell
The single most importantuse is that your code has to have one- i.e. everything you write in C# executes in an AppDomain
. That is quite important ;-p
的一个最重要的用途是你的代码必须有一个-你在C#中执行写的,即一切AppDomain
。这很重要;-p
If you mean additionalapp-domains:
如果您指的是其他应用程序域:
When using plugins and other untrusted code, it allows you both isolation, and the ability to unload them (you can't unload assemblies - only entire app-domains).
当使用插件和其他不受信任的代码时,它允许您隔离和卸载它们(您不能卸载程序集 - 只能卸载整个应用程序域)。
I'm using it currently to load dynamically generated dlls, so that I can unload them.
我目前正在使用它来加载动态生成的 dll,以便我可以卸载它们。
They also allow you to set different configuration files, trust levels, etc - but have associated costs of complexity and remoting.
它们还允许您设置不同的配置文件、信任级别等 - 但会产生相关的复杂性和远程处理成本。
MSDN has a section on app-domains, here.
MSDN 有一个关于应用域的部分,这里。
回答by Brian Rasmussen
I can't tell you what the most important use is, since that depends on the situation.
我不能告诉你最重要的用途是什么,因为这取决于具体情况。
AppDomains are useful for sandboxing parts of your application. You can load extensions in an AppDomain and unload them again - something you cannot otherwise do. You can assign specific rights to AppDomains. Per default objects in different AppDomains cannot access each other.
AppDomains 对应用程序的沙箱部分很有用。您可以在 AppDomain 中加载扩展并再次卸载它们 - 这是您无法做到的。您可以为 AppDomains 分配特定的权限。不同 AppDomain 中的默认对象不能相互访问。
AppDomains can be viewed as lightweight processes as they give you many of the same features. However, unlike a Process new AppDomains do not have their own thread per default. You have to manage AppDomains and threads yourself.
AppDomains 可以被视为轻量级进程,因为它们为您提供了许多相同的功能。但是,与 Process 新 AppDomains 不同,默认情况下没有自己的线程。您必须自己管理 AppDomains 和线程。
Also, AppDomains all share the same managed heap. This is usually not a problem, but it may have surprising effects as some instances like strings are shared among AppDomains. For regular use this is not an issue, but if you use strings for locking, threads in different AppDomain can affect each other.
此外,AppDomains 都共享相同的托管堆。这通常不是问题,但它可能会产生令人惊讶的效果,因为某些实例(如字符串)在 AppDomain 之间共享。对于常规使用,这不是问题,但如果您使用字符串进行锁定,不同 AppDomain 中的线程可能会相互影响。
回答by Hoghweed
In general, it's not so daily coding practice to use AppDomains, this could be considered something as an advanced concept.. but, starting from this simple thing, it's important to better understand concepts behind the word "AppDomain".
一般来说,使用 AppDomains 并不是那么日常的编码实践,这可以被认为是一个高级概念..但是,从这个简单的事情开始,更好地理解“AppDomain”这个词背后的概念很重要。
In terms of architecture, and taking it simple as possible, an AppDomain is an isolation container even in terms of memory addressing, inside it all the assemblies needed by an application are loaded and executed, even if this concept is more complicated to explain in details (I hope it's not about yor question to going so deeper).
就架构而言,尽可能简单,AppDomain 是一个隔离容器,即使在内存寻址方面,应用程序所需的所有程序集都在其中加载和执行,即使这个概念更复杂,无法详细解释(我希望这不是关于你更深入的问题)。
Starting from there, the AppDomain class first of all is used to obtain access to the application related executing application domain, this could be done via the Singleton property implementation AppDomain.CurrentDomain
. In this way it's possible to:
从那里开始,首先使用 AppDomain 类来获取对与应用程序相关的执行应用程序域的访问权限,这可以通过 Singleton 属性实现来完成AppDomain.CurrentDomain
。通过这种方式,可以:
- obtain access the loaded assemblies;
- obtain access to the appdomain-shared data slots;
- intems marshalling, in terms of unwrapping created instances from loaded assemblies in created domains.
- 访问加载的程序集;
- 获得对 appdomain 共享数据槽的访问权;
- 内部编组,在从创建的域中加载的程序集中解包创建的实例方面。
Then, the AppDomain class is used to:
然后,AppDomain 类用于:
- create more "domains" in the same process;
- executing assemblies in the process;
- manage the appdomain's loading/unloading process.
- 在同一过程中创建更多“域”;
- 在过程中执行程序集;
- 管理应用程序域的加载/卸载过程。
It could be useful to take a view of the code of the new Microsoft framework (not yet released) MEF (Managed Extesibility Framework)which is truly based on concepts like AppDomains creations and unload, dynamically loaded assemblies.
查看新的 Microsoft 框架(尚未发布)MEF(托管可扩展性框架)的代码可能很有用,该框架真正基于 AppDomains 创建和卸载、动态加载的程序集等概念。
As a simple example of things and examples of what you can do with AppDomains, I can share this link.
作为您可以使用 AppDomains 执行的操作和示例的简单示例,我可以分享此链接。
I hope I answered your question.
我希望我回答了你的问题。
回答by Shivprasad Ktheitroadala
A C# AppDomain is a logically isolated container inside which .NET code run. When you run any .NET code it always runs in a default appdomain.
AC# AppDomain 是一个逻辑隔离的容器,.NET 代码在其中运行。当您运行任何 .NET 代码时,它总是在默认应用程序域中运行。
Do watch this 30 minutes youtube video What is C# AppDomain ?which explains AppDomain in more detail.
请观看这段 30 分钟的 YouTube 视频C# AppDomain 是什么?其中更详细地解释了 AppDomain。
But let me still try to explain in more detail. Lets say you get a third party DLL and you want to use it in your application. But you also suspect that the third party can have some malicious code so you would like to run the third party DLL in a constrained environment. Like you do not want the third party to access your c: drive or delete files and so on.
但让我仍然尝试更详细地解释。假设您获得了第三方 DLL,并且您想在您的应用程序中使用它。但是您也怀疑第三方可能有一些恶意代码,因此您希望在受限环境中运行第三方 DLL。就像您不希望第三方访问您的 c: 驱动器或删除文件等。
So you can create two AppDomains one which is for the third party and one for your own C# classes. For the third party appdomain you will apply security constraint that it can not access c: drive and for your C# DLLs you will have a unrestricted app domain.
因此,您可以创建两个 AppDomain,一个用于第三方,另一个用于您自己的 C# 类。对于第三方应用程序域,您将应用无法访问 c: 驱动器的安全约束,对于您的 C# DLL,您将拥有不受限制的应用程序域。
回答by vCillusion
Please read my blog for standard application of runtime loading of DLLs and cross-communication using AppDomain. https://blog.vcillusion.co.in/sending-events-through-application-domain-boundary/
请阅读我的博客,了解 DLL 运行时加载和使用 AppDomain 进行交叉通信的标准应用程序。https://blog.vcillusion.co.in/sending-events-through-application-domain-boundary/
- Runtime Loading and unloading of DLLs: I worked on a project where DLLs are loaded at runtime by the user, and during program execution, the methods are executed using Reflection and unloaded during the program run.
- Securing my Main Execution Program: We are loading the DLL dynamically so any exception that occurred in that dynamically loaded DLL didn't affect my main AppDomain. In case of corruption scenarios, we have the option of efficiently unloading and loading the DLL again.
- Cross-AppDomain Communication: We can dynamically load any two DLLs at runtime in different AppDomain and make them communicate with each other.
- DLL 的运行时加载和卸载:我参与了一个项目,其中 DLL 在运行时由用户加载,在程序执行期间,这些方法使用反射执行并在程序运行期间卸载。
- 保护我的主执行程序:我们正在动态加载 DLL,因此在该动态加载的 DLL 中发生的任何异常都不会影响我的主 AppDomain。如果出现损坏情况,我们可以选择有效地卸载和再次加载 DLL。
- 跨 AppDomain 通信:我们可以在运行时动态加载不同 AppDomain 中的任意两个 DLL,并使它们相互通信。