The host can create application domains to run user code
with security, isolation and unloading. It is not a better idea to run user
code in the default domain. Let us list down the reasons for the same:
·
A default domain cannot be unloaded until the process exists
·
Security Isolation
Now that the host has created an application domain, we can
execute user code in the domain. All code that is run in the CLR must be part of
an assembly. An assembly is the deployment unit for types and resources and is
the primary unit of execution in the CLR. The manner in which assemblies are
loaded for execution depends largely on the host's specific scenario.
In general, there are two options:
·
The first option is to load precompiled assemblies from a disk.
·
The second option is to create assemblies on the fly using the
API's in the System.Reflection.Emit namespace.
The code shown in Listing 2 is for creating an
ApplicationDomain and executing an assembly into it. The code is written in C#.
The Assembly that is loaded is an exe file.
Listing 2
ObjAppDomain = AppDomain.CreateDomain("My Application 1");
ObjAppDomain.ExecuteAssembly(@"C:\APPDOMAIN_Test.exe");
The code shown below is for unloading the Application domain
from the memory.
Listing 3
AppDomain.Unload(ObjAppDomain);