Whereas the assembly is a logical unit of deployment, the AppDomain is a logical unit of execution. The AppDomain is a sandbox in which .NET code runs. The AppDomain provides a container in which code can execute safely, knowing that code running outside the AppDomain cannot negatively impact it. As you will see throughout this book, cross-process communication done with Remoting is actually the process of passing information between AppDomains, whether they are on the same machine or different machines.
Throughout the course of an application, your code may load a lot of assemblies creating instances of other classes. Without the use of the AppDomain class, all of those assemblies will remain in memory for the lifespan of your application. If you are coding a long-running server application, this is unacceptable. Using the AppDomain class, however, you can manually control the loading and unloading of assemblies, as well as create an isolated environment to run potentially volatile tasks.
To illustrate how to program with AppDomains, we are going to create a
solution that has a console application and a class library. The code in the
console application is going to create a new AppDomain and use that new
AppDomain to execute code within the class library. When that secondary
code is finished executing, the code in the console application will then unload
the temporary AppDomain, illustrating how you can gain tight control
over the memory consumption of your application by manually loading and
unloading certain AppDomains.
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Text;
namespace SecondaryCode
{
public class Class1 : MarshalByRefObject
{
public void DoWork()
{
Console.WriteLine("[{0}] Doing some work.",
AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("[{0}] Domain-Wide value is {1}",
AppDomain.CurrentDomain.FriendlyName,
AppDomain.CurrentDomain.GetData("THEVALUE"));
Console.WriteLine("This domain currently has {0} Assemblies loaded.",
AppDomain.CurrentDomain.GetAssemblies().Length.ToString());
foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
{
Console.WriteLine("\t{0}", a.GetName().Name);
}
}
}
}
namespace AppDomainSample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("[{0}] Main Application starting.",
AppDomain.CurrentDomain.FriendlyName) ;
Console.WriteLine("Main AppDomain has {0} Assemblies Loaded.",
AppDomain.CurrentDomain.GetAssemblies().Length.ToString());
AppDomain ad = AppDomain.CreateDomain("WorkerDomain");
ad.SetData("THEVALUE", "How much wood could a woodchuck chuck...");
SecondaryCode.Class1 remoteType =
(SecondaryCode.Class1)ad.CreateInstanceFromAndUnwrap("SecondaryCode.dll",
"SecondaryCode.Class1");
remoteType.DoWork();
AppDomain.Unload(ad);
Console.ReadLine();
}
}
}
ReplyDeleteHi there, awesome site. I thought the topics you posted on were very interesting. I tried to add your RSS to my feed reader and it a few. take a look at it, hopefully
I can add you and follow.
Dot Net Training in Chennai