Application Domains and Services
Creating Application Domains (AD)
-
‘Application domain is an logical contrainer that allows multiple assemblies to turn within a single process but prevents them from directly accessing memeory that belongs to other assemblies.’
-
separating unhandled exceptions within one assembly do not affect another application domains.
-
different security access level can be assigned on each application domains.
.NET Framework runtime manage the application domains, whereas OS manage the processes.
As what the figure is showing, Application domains provides separate memory spaces and separate access to resources. But, application domain allow running multiple assemblies within separate application domains which are in one process.
If the application (I think it means the assemblies) is trust completely, it can bypass .NET framework security checks by calling native code. That means it can accessing anything within the process.
One example of usage of application domains is IIS ASP.NET worker process (w3wp.exe), it allow multiple ASP.NET applications to be run on a single Web server. ASP.NET create separate application domain for each application to avoid conflict. To communicating, use the .NET remoting, Web Services or other similar techniques.
Use application domain can improve the Reliability, use AD to isolate those tasks that might cause a process terminate. The AD can be unloaded withouth affecting process. This technique is suitable for a process that need run a long period. For example, the Add-in can be put into a separate AD and unloaded whenever you want but without influcing the parent application domain.
About Efficiency, if an assembly is loaded into second AD ( not the default one ),this assembly can be unloaded when this second AD is unloaded. Therefore, it can reduce the using of resource like memory. Using this method can optimize the long-running process that occasionally use large DLLs.
AppDomian Class: System.AppDomian. To use AD, create an object of AppDomian, and then execute an assembly within that domain (AppDomian object).
Create an Application Domain & load an assembly into this AD:
1: AppDomain d = AppDomain.CreateDomain("NewDomain");
2: d.ExecuteAssembly("Assembly.exe"); //can pass command-line arguments
3:
4: //or
5:
6: AppDomain d = AppDomain.CreateDomain("NewDomain");
7: d.ExecuteAssemblyByName("Assembly"); //use a reference
Unload an AD:
1: // must unload a AD, individual assemblies or types cannot be unloaded.
2: AppDomain d = AppDomain.CreateDomain("NewDomain");
3: AppDomain.Unload(d);
以上
- 我写的不是代码 是寂寞
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器