Application Domain in .NET Framework 2.0

Waqas Anwar
02 March 2008
10887 Views

.NET Developers often need to run an external assembly. However, running an external assembly can be a risky operation as developers have no guarantee that whether that external assembly will execute without any security breaches or not. Inefficient resource usage such as printers or file system from external assemblies can also create problems for applications. In .NET Framework Microsoft provides excellent solution to solve this problem in the form of “Application Domain”.

Application domain is a logical location in memory where a process runs.  When .NET CLR execute .NET assembly it creates the main application process and then .NET developers create more small application domains to run external .NET assemblies separately then the main application process.


AppDomain.gif

Application Domain keeps assemblies separate within a single process.




The following code shows you how to create Application Domain in .NET and how to execute an external assembly inside domain.


C#

// Creating Application Domain
AppDomain domain = AppDomain.CreateDomain("MyDomain");


// Loading Assembly in Application Domain
domain.ExecuteAssembly("../../TestAssembly.exe");
   

// Unload Application Domain
AppDomain.Unload(domain);


VB.NET

'Creating Application Domain
Dim domain As AppDomain = AppDomain.CreateDomain("MyDomain")


'Loading Assembly in Application Domain
domain.ExecuteAssembly("../../TestAssembly.exe")


'Unload Application Domain
'AppDomain.Unload(domain)


Application domains not only isolate .NET assemblies for security reasons but also improve reliability and efficiency of .NET applications.