简体   繁体   English

.NET中应用程序域的常见用法和最佳做法?

[英]Common uses and best practices for application domains in .NET?

What are some guidelines and best practices for when to create new application domains within an application? 什么时候在应用程序中创建新的应用程序域有哪些准则和最佳实践?

Also, what are some common uses and examples of how multiple application domains are used whithin an application? 另外,在一个应用程序中如何使用多个应用程序域,有哪些常见用法和示例?

The most common scenario I've seen is to be able to provide extensibility with a different security model than the main program. 我见过的最常见的情况是能够以与主程序不同的安全模型提供可扩展性。

Loading a plugin in a separate AppDomain allows two things: 在单独的AppDomain中加载插件有两件事:

  1. You can implement a more restricted security model 您可以实施更严格的安全模型
  2. You can prevent the plugin from tearing down your application if it's buggy 如果有错误,您可以防止该插件破坏您的应用程序

Another nice use of AppDomains are to load and inspect an assembly. AppDomain的另一个很好的用法是加载和检查程序集。 Doing this in a separate AppDomain allows you to glean information (or run code) from a separate assembly, then unload the entire assembly from your process's space. 在单独的AppDomain中执行此操作可让您从单独的程序集中收集信息(或运行代码),然后从流程空间中卸载整个程序集。 If you load the assembly directly, there is no way to unload it. 如果直接加载装配件,则无法卸载它。 This is also useful if you want to be able to, at runtime, "upgrade" a type to a new version (ie: load a remote assembly, and reload it later). 如果希望在运行时将类型“升级”到新版本(即:加载远程程序集,然后再重新加载),这也很有用。

当需要在应用程序中托管不可靠的第三方组件或不信任它们(例如插件)或希望能够卸载它们时,建议创建新域。

A typical example is for plugin-/addin-like cases. 一个典型的例子是类似插件/插件的情况。 Not only does it allow you to unload the DLL if required, it also gives you better security control over what the plugin is allowed to do. 它不仅可以让您根据需要卸载DLL,还可以更好地控制插件的安全性。

Also, if you create temporary assemblies (code generation) which you want to unload again, this is a good way to do it. 另外,如果您创建了要再次卸载的临时程序集(代码生成),这是一个很好的方法。 (LCG only allows implementing single methods, if you want to implement a complete class you need to emit to a "real" assembly). (LCG仅允许实现单个方法,如果要实现一个完整的类,则需要向“实际”程序集发出该方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM