C# 中 AppDomain 的使用
C# 中 AppDomains 最重要的用途是什么?
What is the most important use of AppDomains in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
C# 中 AppDomains 最重要的用途是什么?
What is the most important use of AppDomains in C#?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
最重要的用途是您的代码必须有一个 - 即您用 C# 编写的所有内容都在
AppDomain
中执行。 这非常重要;-p如果您指的是其他应用程序域:
当使用插件和其他不受信任的代码时,它允许您隔离,并且能够卸载它们(您无法卸载程序集 -仅整个应用程序域)。
我目前正在使用它来加载动态生成的 dll,以便我可以卸载它们。
它们还允许您设置不同的配置文件、信任级别等,但会带来复杂性和远程处理的相关成本。
MSDN 有一个关于应用程序域的部分,此处。
The single most important use is that your code has to have one - i.e. everything you write in C# executes in an
AppDomain
. That is quite important ;-pIf you mean additional app-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.
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.
我无法告诉你最重要的用途是什么,因为这取决于具体情况。
AppDomain 对于应用程序的沙箱部分非常有用。 您可以在 AppDomain 中加载扩展并再次卸载它们 - 这是您无法执行的其他操作。 您可以向 AppDomain 分配特定权限。 默认情况下,不同 AppDomain 中的对象无法相互访问。
AppDomain 可以被视为轻量级进程,因为它们为您提供许多相同的功能。 但是,与进程不同,新的 AppDomain 默认情况下没有自己的线程。 您必须自己管理 AppDomain 和线程。
此外,AppDomain 都共享相同的托管堆。 这通常不是问题,但它可能会产生令人惊讶的效果,因为某些实例(例如字符串)在 AppDomain 之间共享。 对于常规使用,这不是问题,但如果您使用字符串进行锁定,不同 AppDomain 中的线程可能会相互影响。
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 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.
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.
一般来说,使用 AppDomain 并不是日常编码实践,这可以被认为是一个高级概念。但是,从这个简单的事情开始,更好地理解“AppDomain”一词背后的概念很重要。
就架构而言,尽可能简单地说,即使在内存寻址方面,AppDomain 也是一个隔离容器,在其中加载并执行应用程序所需的所有程序集,即使这个概念详细解释起来比较复杂(我希望这不是关于你的问题,需要更深入)。
从这里开始,AppDomain 类首先用于获取对与应用程序相关的执行应用程序域的访问,这可以通过 Singleton 属性实现 AppDomain.CurrentDomain 来完成。 通过这种方式,可以:
然后,AppDomain类用于:
查看新的 Microsoft 框架(尚未发布)MEF(托管可扩展性框架)的代码可能会很有用 它真正基于 AppDomains 创建和卸载、动态加载程序集等概念。
作为一个简单的示例以及您可以使用 AppDomains 执行哪些操作的示例,我可以分享这个
我希望我回答了你的问题。
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".
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).
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:Then, the AppDomain class is used to:
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.
As a simple example of things and examples of what you can do with AppDomains, I can share this link.
I hope I answered your question.
AC# AppDomain 是一个逻辑隔离的容器,.NET 代码在其中运行。 当您运行任何 .NET 代码时,它始终在默认应用程序域中运行。
请观看这个 30 分钟的 YouTube 视频 什么是 C# AppDomain ? 更详细地解释了 AppDomain。
但让我仍然尝试更详细地解释一下。 假设您获得了第三方 DLL 并且想要在应用程序中使用它。 但您也怀疑第三方可能有一些恶意代码,因此您希望在受限环境中运行第三方 DLL。 比如您不希望第三方访问您的 c: 驱动器或删除文件等。
因此,您可以创建两个 AppDomain,一个用于第三方,一个用于您自己的 C# 类。 对于第三方应用程序域,您将应用安全约束,使其无法访问 c: 驱动器,而对于 C# DLL,您将拥有不受限制的应用程序域。
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.
Do watch this 30 minutes youtube video What is C# AppDomain ? which explains AppDomain in more detail.
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.
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.
请阅读我的博客,了解 DLL 运行时加载和使用 AppDomain 进行交叉通信的标准应用。 https://blog.vcillusion.co.in/sending -events-through-application-domain-boundary/
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/