CHAPTER 17 PROCESSES, APPDOMAINS, AND OBJECT CONTEXTS
newAD.DomainUnload += (o, s) =>
{
Console.WriteLine("The second AppDomain has been unloaded!");
};
try
{
// Now load CarLibrary.dll into this new domain.
newAD.Load("CarLibrary");
}
catch (FileNotFoundException ex)
{
Console.WriteLine(ex.Message);
}
// List all assemblies.
ListAllAssembliesInAppDomain(newAD);
}
// Now tear down this AppDomain.
AppDomain.Unload(newAD);
If you want to be notified when the default application domain is unloaded, modify your Main()
method to handle the ProcessEvent event of the default application domain, like so:
static void Main(string[] args)
{
Console.WriteLine("***** Fun with Custom AppDomains *****\n");
// Show all loaded assemblies in default AppDomain.
AppDomain defaultAD = AppDomain.CurrentDomain;
defaultAD.ProcessExit += (o, s) =>
{
Console.WriteLine("Default AD unloaded!");
};
ListAllAssembliesInAppDomain(defaultAD);
}
MakeNewAppDomain();
Console.ReadLine();
That wraps up our look at the .NET application domain. To conclude this chapter, let’s look at one
further level of partitioning, which is used to group objects into contextual boundaries.
Source Code The CustomAppDomains project is included under the Chapter 17 subdirectory.
645