C# 中 AppDomain 的使用

发布于 2024-07-15 07:35:44 字数 33 浏览 7 评论 0原文

C# 中 AppDomains 最重要的用途是什么?

What is the most important use of AppDomains in C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

睫毛上残留的泪 2024-07-22 07:35:44

最重要的用途是您的代码必须有一个 - 即您用 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 ;-p

If 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.

月亮坠入山谷 2024-07-22 07:35:44

我无法告诉你最重要的用途是什么,因为这取决于具体情况。

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.

¢蛋碎的人ぎ生 2024-07-22 07:35:44

一般来说,使用 AppDomain 并不是日常编码实践,这可以被认为是一个高级概念。但是,从这个简单的事情开始,更好地理解“AppDomain”一词背后的概念很重要。

就架构而言,尽可能简单地说,即使在内存寻址方面,AppDomain 也是一个隔离容器,在其中加载并执行应用程序所需的所有程序集,即使这个概念详细解释起来比较复杂(我希望这不是关于你的问题,需要更深入)。

从这里开始,AppDomain 类首先用于获取对与应用程序相关的执行应用程序域的访问,这可以通过 Singleton 属性实现 AppDomain.CurrentDomain 来完成。 通过这种方式,可以:

  1. 访问已加载的程序集;
  2. 获取对 appdomain 共享数据槽的访问权限;
  3. intems 编组,即从创建的域中加载的程序集中解包创建的实例。

然后,AppDomain类用于:

  1. 在同一进程中创建更多“域”;
  2. 在进程中执行程序集;
  3. 管理应用程序域的加载/卸载过程。

查看新的 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:

  1. obtain access the loaded assemblies;
  2. obtain access to the appdomain-shared data slots;
  3. intems marshalling, in terms of unwrapping created instances from loaded assemblies in created domains.

Then, the AppDomain class is used to:

  1. create more "domains" in the same process;
  2. executing assemblies in the process;
  3. 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.

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.

孤云独去闲 2024-07-22 07:35:44

AC# AppDomain 是一个逻辑隔离的容器,.NET 代码在其中运行。 当您运行任何 .NET 代码时,它始终在默认应用程序域中运行。

请观看这个 30 分钟的 YouTube 视频 什么是 C# AppDomain ? 更详细地解释了 AppDomain。

C# 应用程序域

但让我仍然尝试更详细地解释一下。 假设您获得了第三方 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.

C# 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.

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.

天邊彩虹 2024-07-22 07:35:44

请阅读我的博客,了解 DLL 运行时加载和使用 AppDomain 进行交叉通信的标准应用。 https://blog.vcillusion.co.in/sending -events-through-application-domain-boundary/

  1. 运行时加载和卸载 DLL:我从事过一个项目,其中 DLL 由用户在运行时加载,在程序执行期间,方法使用反射执行并在程序运行期间卸载。
  2. 保护我的主执行程序:我们动态加载 DLL,因此动态加载的 DLL 中发生的任何异常都不会影响我的主 AppDomain。 如果发生损坏情况,我们可以选择有效地卸载并再次加载 DLL。
  3. 跨AppDomain通信:我们可以在运行时动态加载不同AppDomain中的任意两个DLL,并使它们相互通信。

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/

  1. 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.
  2. 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.
  3. Cross-AppDomain Communication: We can dynamically load any two DLLs at runtime in different AppDomain and make them communicate with each other.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文