2. Introduction
• Application domain (other name appDomain) is
nothing but one logical region where .NET
runtime runs and execute code.
• Multiple application domains can exist in one
Win32 process.
• It provides security and isolation for executing
managed code.
• We can unload any task without affecting the
process.
• The .NET runtime force application domain
isolation by keeping control over memory.
3. System.AppDomain Class(1)
• The AppDomain class is used
to create and terminate
application domains, load and
unload assemblies and types,
and enumerate assemblies
and threads in a domain.
• The following table shows
some useful methods of the
AppDomain class as in the
following;
4. System.AppDomain Class(2)
• In addition, the AppDomain
class also defines as set of
properties that can be useful
when you wish to monitor
activity of a given
application domain
5. Display application domain name and
assembly nameusing System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Reflection;
namespace TestDomain
{
class Program
{
public static void Main(String[] args)
{
string callingDomainName = Thread.GetDomain().FriendlyName;
Console.WriteLine(callingDomainName);
//Get and display the full name of the EXE assembly.
string exeAssembly = Assembly.GetEntryAssembly().FullName;
Console.WriteLine(exeAssembly);
Console.ReadLine();
}
} }
6. Create application domain (1)
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Reflection;
namespace TestDomain
{
class Program
{
public static void Main(String[] args)
{
//Create an new Application Domain:
System.AppDomain newDomain = System.AppDomain.CreateDomain("NewApplicationDomain");
//Load and execute an assembly:
newDomain.ExecuteAssembly(@"C:/Users/user/Documents/visual studio
2010/Projects/HelloWorld/HelloWorld/bin/Debug/HelloWorld.exe");
//Unload the application domain:
System.AppDomain.Unload(newDomain);
Console.ReadLine();
}
7. Create application domain (2)
using System;
using System.Reflection;
//add a reference to AppDoaminTest.exe
namespace DemoTest
{
class Program
{
static void Main(string[] args)
{
AppDomain d1 = AppDomain.CurrentDomain;
Console.WriteLine(d1.FriendlyName);
AppDomain d2 = AppDomain.CreateDomain("New AppDomain");
d2.ExecuteAssembly(@"C:/Users/user/Documents/visual studio
2010/Projects/HelloWorld/HelloWorld/bin/Debug/HelloWorld.exe");
}
}
}
8. Loading Assemblies into Custom
Application Domain (1)
• CLR will always load assemblies into the default application domain when
required.
• If you wanted to manually load assemblies into an application domain,
then you can do that by the AppDomain.Load() method.
• Here, suppose that you want to load a previously create library
ClassLibrary5.dll into a secondary application domain.
AppDomain.GetAssemblies Method ()
Gets the assemblies that have been loaded into the execution context of this
application domain.
9. Loading Assemblies into Custom
Application Domain (2)
using System;
using System.IO;
using System.Linq;
using ClassLibrary5;
namespace DemoTest
{
class Program
{
static void Main(string[] args)
{
AppDomain newDomain = AppDomain.CreateDomain("New AppDomain");
try
{
newDomain.Load("ClassLibrary5");
}
catch (FileNotFoundException)
{
Console.WriteLine("Not Found");
}
ListAssemblies(newDomain);
Console.ReadKey();
}
static void ListAssemblies(AppDomain ad)
{
var la = from a in ad.GetAssemblies()
orderby a.GetName().Name
select a;
Console.WriteLine("Assemblies Loaded {0}n", ad.FriendlyName);
foreach (var a in la)
{
Console.WriteLine("Name:: {0}:", a.GetName().Name);
Console.WriteLine("Version:: {0}:n", a.GetName().Version);
}
}
}
}