Application Domains (C# Programming Guide)

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.

static void Main()
{
    // 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# Programming Guide).

  • 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.

C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 3.1 Application Startup

See Also

Concepts

C# Programming Guide

Reference

Assemblies and the Global Assembly Cache (C# Programming Guide)