Vous êtes sur la page 1sur 9

Appdomains

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.

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;

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

Displayapplication domain name


and assembly name
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Runtime.CompilerServices;
System.Text;
System.Threading;
System.Threading.Tasks;
System.Diagnostics;
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();
}
} }

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();
}

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");
}
}
}

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.

Loading Assemblies into Custom


Application Domain (2)
using
using
using
using

System;
System.IO;
System.Linq;
ClassLibrary5;

static void ListAssemblies(AppDomain ad)


{
var la = from a in ad.GetAssemblies()
orderby a.GetName().Name
select a;

namespace DemoTest
{
Console.WriteLine("Assemblies Loaded {0}\n",
class Program
ad.FriendlyName);
{
static void Main(string[] args)
foreach (var a in la)
{
{
AppDomain newDomain = AppDomain.CreateDomain("New
Console.WriteLine("Name:: {0}:",
AppDomain");
a.GetName().Name);
try
Console.WriteLine("Version:: {0}:\n",
{
a.GetName().Version);
newDomain.Load("ClassLibrary5");
}
}
}
catch (FileNotFoundException)
}
{
}
Console.WriteLine("Not Found");
}
ListAssemblies(newDomain);
Console.ReadKey();
}

Vous aimerez peut-être aussi