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
   4:  //or
   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);

 - 我写的不是代码 是寂寞
posted @ 2009-08-20 09:01  我写的不是代码 是寂寞  阅读(393)  评论(0编辑  收藏  举报