App. domains (AppDomain) implementation with C# and .Net Framework

App. domains allow us to run Assemblies within same process space but without direct access to other assemblies’ memories. The main advantage of app. domains over separate processes is that app. domains don’t require too much of start up cost as processes do.

Good example is IIS app. domains that create separate app. domains for each user hitting IIS process. In other word, IIS creates separate instances of assembly for each user. Another advantage of App. domains is that you can call of Assemblies from within other Assemblies and access only certain parts of assemblies you are calling on. App. domain is also very reliable. For instance, if you need to run a process for longer period of time you can ensure it by creating app. domain and running most unstable code within this domain. This way if code fails all is going to happen is App. domain will be unloaded instead of process. Efficiency is another advantage, if you run assembly within a process you can not upload Assembly while process is running, but with App. domain, Assembly us unloaded as soon as App. domain is unloaded.

AppDomain Properties


ActivationContext Gets the activation context
ApplicationIdentity Gets the identity of the application
ApplicationTrust Gets information about permissions granted to an application
BaseDirectory Gets the base directory that the assembly resolver
CurrentDomain Gets the current app. domain for the current Thred
DomainManager Gets the domain manager that was provided by the host
DynamicDirectory Gets the directory that the assembly resolver
Evidence Gets the Evidence associated with this app. domain
FriendlyName Gets the friendly name of this app. domain
Id Gets an integer that uniquely identifies the app. domain
RelativeSearchPath Gets the path relative to the base directory
SetupInformation SetupInformation
ShadowCopyFiles Gets an indication whether all assemblies loaded in the shows an indication if assembly refences are being cached or not



ApplyPolicy Returns the assembly display
CreateComInstanceFrom Creates a new instance of a COM type
CreateDomain Creates a new app. domain
CreateInstance Creates a new instance of a specified type
CreateInstanceAndUnwrap Creates a new instance of a specified type
CreateInstanceFrom Creates a new instance of a specified type defined in the specified assembly
CreateInstanceFromAndWrap Creates a new instance of a specified type defined in the specified assembly file
DefineDynamicAssembly Defines a dynamic assembly
DoCallBack Executes the code in another app. domain
ExecuteAssembly Executes the assembly contained in the specified file
ExecuteAssemblyByName Executes an assembly
GetAssemblies Gets the assemblies that have been loaded
GetCurrentThreadId Gets the current thread identifier
GetData Gets the value stored in the current app. domain
InitializeLifetimeService Gives the AppDomain an infinite lifetime
IsDefaultAppDomain Returns a value that indicates if the app. domain is the default
IsFinalizingForUnload Indicates whether this app. domain is unloading
Load Loads an Assembly into this app. domain
ReflectionOnlyGetAssemblies Gets the assemblies that have been loaded into the reflection-only context of the app. domain
SetAppDomainPolicy Sets the security policy level for this app. domain
SetData Assigns a value to an app. domain property
SetDynamicBase Sets the specified directory path as the location
SetPrincipalPolicy Sets how principal and identity objects should be attached to a thread
SetShadowCopyFiles Sets on shadow copying
SetShadowCopyPath Sets the specified directory path
SetThreadPrincipal Sets the default principal object to be attached to threads
Unload Unloads the specified app. domain

AppDomain myD = AppDomain.CreateDomain("NewDomain");//create domain
myD.ExecuteAssembly("Assembly.exe");//load Assembly.exe
myD.ExecuteAssemblyByName("Assembly ");//load Assembly
AppDomain.Unload(myD);//unload assembly