Launcher: Mastering Your Own Domain
by Gigi Sayfan

Listing One

// The post build event
call "$(DevEnvDir)..\Tools\vsvars32.bat"
sn -Vr $(TargetDir)$(TargetFileName)
gacutil -u $(TargetName)
gacutil -i $(TargetDir)$(TargetFileName)

// From AssemblyInfo.cs
[assembly: AssemblyKeyFileAttribute("..\\..\\public.snk")]
[assembly: AssemblyDelaySignAttribute(true)]


Listing Two

namespace DomainMasterSample
{
   class MultiDomainApp
   {
      [LoaderOptimization(LoaderOptimization.MultiDomainHost)]
      static void Main() 
      {
      }
}

Listing Three

namespace DomainMasterSample
{
   class MultiDomainApp
   {
      [LoaderOptimization(LoaderOptimization.MultiDomainHost)]
      static void Main() 
      {
         // Create a new AppDoamin
         AppDomain ad = AppDomain.CreateDomain("Playground Domain");         
         // Unload the domain
         AppDomain.Unload(ad);         
      }

   }
}

Listing Four

namespace DomainMasterSample
{
   class MultiDomainApp
   {
      [LoaderOptimization(LoaderOptimization.MultiDomainHost)]
      static void Main() 
      {
         // Create a new AppDomain
         Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
         AppDomain ad = AppDomain.CreateDomain("Playground Domain");         
         // Register domain event handlers
         ad.AssemblyLoad += new AssemblyLoadEventHandler(AssemblyLoadHandler);
         ad.DomainUnload += new EventHandler(DomainUnloadHandler);
         // Load and execute assembly in domain         
         ad.ExecuteAssembly("Playground.exe");
         // Unload the domain
         AppDomain.Unload(ad);         
      }
      static public void AssemblyLoadHandler(object sender, 
                                                 AssemblyLoadEventArgs args) 
      {
         AppDomain ad = sender as AppDomain;
         Debug.Assert(ad == AppDomain.CurrentDomain);
         string a = args.LoadedAssembly.GetName().Name;         
         AppDomain c = ad.GetData("Creator") as AppDomain;
         Console.WriteLine("Assembly Loaded: '{0}' in '{1}' 
                     created by '{2}'", a, ad.FriendlyName, c.FriendlyName);
      }
      static public void DomainUnloadHandler(object sender, EventArgs args) 
      {
         AppDomain ad = sender as AppDomain;
         Debug.Assert(ad == AppDomain.CurrentDomain);         
      }
   }
}


Listing Five

namespace DomainMasterSample
{
   class MultiDomainApp
   {
      [LoaderOptimization(LoaderOptimization.MultiDomainHost)]
      static void Main() 
      {
         // Create a new AppDomain
         Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
         AppDomain ad = AppDomain.CreateDomain("Playground Domain");         
         // Register domain event handlers
         ad.AssemblyLoad += new AssemblyLoadEventHandler(AssemblyLoadHandler);
         ad.DomainUnload += new EventHandler(DomainUnloadHandler);
         // Load and execute assembly in domain         
         ad.ExecuteAssembly("Playground.exe");
         // Unload the domain
         AppDomain.Unload(ad);         
      }
      static public void AssemblyLoadHandler(object sender, 
                                                  AssemblyLoadEventArgs args) 
      {
         AppDomain ad = sender as AppDomain;
         Debug.Assert(ad == AppDomain.CurrentDomain);
         string a = args.LoadedAssembly.GetName().Name;         
         AppDomain c = ad.GetData("Creator") as AppDomain;
         Console.WriteLine("Assembly Loaded: '{0}' in '{1}' 
                      created by '{2}'", a, ad.FriendlyName, c.FriendlyName);
      }
      static public void DomainUnloadHandler(object sender, EventArgs args) 
      {
         AppDomain ad = sender as AppDomain;
         Debug.Assert(ad == AppDomain.CurrentDomain);         
      }
   }
}

Listing Six

namespace DomainMasterSample
{
   class MultiDomainApp
   {
      [LoaderOptimization(LoaderOptimization.MultiDomainHost)]
      static void Main() 
      {
         // Create a new AppDomain
         Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
         AppDomain ad = AppDomain.CreateDomain("Playground Domain");         
         // Exchange data between AppDomains
         ad.SetData("Creator", AppDomain.CurrentDomain);
         AppDomain creator = ad.GetData("Creator") as AppDomain;
         Debug.Assert(creator == AppDomain.CurrentDomain);
         // Unload the domain
         AppDomain.Unload(ad);         
      }
   }
}

Listing Seven

namespace DomainMasterSample
{
   class MultiDomainApp
   {
      [LoaderOptimization(LoaderOptimization.MultiDomainHost)]
      static void Main() 
      {
         // Create a new AppDomain
         Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
         AppDomain ad = AppDomain.CreateDomain("Playground Domain");         
         // Exchange data between AppDomains
         ad.SetData("Creator", AppDomain.CurrentDomain);
         // Execute some code in a different AppDomain
         CrossAppDomainDelegate cadd = 
                     new CrossAppDomainDelegate(CrossAppDomainCallback);
         ad.DoCallBack(cadd);
         // Unload the domain
         AppDomain.Unload(ad);         
      }
      static public void CrossAppDomainCallback()
      {
         AppDomain ad = AppDomain.CurrentDomain;
         AppDomain c = ad.GetData("Creator") as AppDomain;
         Console.WriteLine("CrossAppDomainCallback() running in '{0}' 
                       created by '{1}'", ad.FriendlyName, c.FriendlyName);
      }
   }
}



Listing Eight

namespace DomainMasterSample
{
   public class AppDomainExplorer : MarshalByRefObject
   {
      public void Explore()
      {
         AppDomain ad = AppDomain.CurrentDomain;
         Console.WriteLine("----- AppDomainExplorer.Explore() -----");
         Console.WriteLine("Name: {0}", ad.FriendlyName);
         Console.WriteLine("Setup Info: {0}", ad.SetupInformation.ToString());
         Console.WriteLine("Assemblies:");
         foreach (Assembly a in ad.GetAssemblies())
         {
            Console.WriteLine(a.FullName);
         }
      }
   }
   class MultiDomainApp
   {
      [LoaderOptimization(LoaderOptimization.MultiDomainHost)]
      static void Main() 
      {
         // Create a new AppDomain
         Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
         AppDomain ad = AppDomain.CreateDomain("Playground Domain");
         // Create object in another AppDomain
         string path = Path.Combine(Directory.GetCurrentDirectory(), 
                                                 "DomainMasterSample.exe");
         Assembly a = Assembly.LoadFile(path);
         ad.Load(a.GetName());
         AppDomainExplorer ade = 
                ad.CreateInstanceAndUnwrap(a.GetName().FullName, 
                "DomainMasterSample.AppDomainExplorer") as AppDomainExplorer;
         ade.Explore();
         // Unload the domain
         AppDomain.Unload(ad);
         Console.ReadLine();
      }
   }
}








4



