Programming with AppDomains

Programming with AppDomains

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. The code for the class library is shown in Listing 12.5.

Listing 12.5. A Class Library with Simple Code for Displaying the AppDomain's Loaded Assembly List

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

Listing 12.6 shows the code that creates a new AppDomain and executes code within it.

Listing 12.6. Creating and Unloading an AppDomain

using System; using System.Reflection; using System.Collections.Generic; using System.Text; 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(); } } } 

To prove that the work is being executed in a separate AppDomain, the output of the preceding code looks as follows:

[AppDomainSample.vshost.exe] Main Application starting. Main AppDomain has 11 Assemblies Loaded. [WorkerDomain] Doing some work. [WorkerDomain] Domain-Wide value is How much wood could a woodchuck chuck... This domain currently has 3 Assemblies loaded.         mscorlib         Microsoft.VisualStudio.HostingProcess.Utilities         SecondaryCode 

Working with AppDomains may not be something that you do every day as a .NET developer, but being armed with the knowledge of how they work and how they fit into the overall architecture of the framework will help you create better applications.

Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298 © 2008-2017.
If you may any questions please contact us: