Application Domains (C# and Visual Basic)
Application domains provide a flexible and secure method of isolating running applications.
Application domains are usually created and manipulated by run-time hosts. Occasionally, you may want your application to programmatically interact with your application domains, for example, to unload a component without having to stop your application from running.
Application domains aid security, separating applications from each other and each other's data. A single process can run several application domains, with the same level of isolation that would exist in separate processes. Running multiple applications within a single process increases server scalability.
In the following code example, you create a new application domain and then load and execute a previously built assembly, HelloWorld.exe, that is stored on drive C.
' Create an Application Domain: Dim newDomain As System.AppDomain = System.AppDomain.CreateDomain("NewApplicationDomain") ' Load and execute an assembly: newDomain.ExecuteAssembly("c:\HelloWorld.exe") ' Unload the application domain: System.AppDomain.Unload(newDomain)
// Create an Application Domain: System.AppDomain newDomain = System.AppDomain.CreateDomain("NewApplicationDomain"); // Load and execute an assembly: newDomain.ExecuteAssembly(@"c:\HelloWorld.exe"); // Unload the application domain: System.AppDomain.Unload(newDomain);
Application Domains Overview
Application domains have the following properties:
An assembly must be loaded into an application domain before it can be executed. For more information, see Assemblies and the Global Assembly Cache (C# and Visual Basic).
Faults in one application domain cannot affect other code running in another application domain.
Individual applications can be stopped and code unloaded without stopping the entire process. You cannot unload individual assemblies or types, only entire application domains.