Introduction
Application domain is a construct in the CLR that is the unit of isolation for an application. Application domains provide a secure and versatile unit of processing that the common language runtime can use to provide isolation between applications. You can run several application domains in a single process with the same level of isolation that would exist in separate processes, but without incurring the additional overhead of making cross-process calls or switching between processes. The ability to run multiple applications within a single process dramatically increases server scalability.
Example
using System;
using System.Reflection;
using System.Runtime.Remoting;
public class CallingAssembly
{
public static void Main(string[] args)
{
AppDomainSetup objADS = new AppDomainSetup();
objADS.ApplicationBase = "file:///" + Environment.CurrentDirectory;
AppDomain objAD = AppDomain.CreateDomain("RemoteDomainExample", null, objADS);
objAD.ExecuteAssembly("CalledAssembly.exe");
AppDomain.Unload(objAD);
}
}
Two Application domains interact with each other through Remoting. AppDomain class is derived from MarshalByRefObject, which Enables access to objects across application domain boundaries in applications that support remoting. CreateDomain creates a new application domain with the given name using the assembly binding information and supplied evidence. ExecuteAssembly method of AppDomain, Executes the assembly contained in the specified file in that domain.
Example
using System;
using System.Threading;
public class CalledAssembly
{
public static void Main(string[] args)
{
Console.WriteLine("Called Assembly Application Domain: " + Thread.GetDomain().FriendlyName);
Console.WriteLine("Assembly Called");
}
}
Output
Now CalledAssembly is running in the Remote Domain instead of the default domain.