Running Assemblies in custom application domains

Our console application will have main method with these lines which will display the the current ApplicationDomain name and also the BaseDirectory. 

 Console.Clear();
       Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine(string.Format("This Domain Application Base Is :{0}" + Environment.NewLine + "This Domain  Friendly Name Is :{1}", AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain.FriendlyName));
        Console.WriteLine(Environment.NewLine + "Pess any key to continue...");
        Console.ReadKey(false);

Just run the application with above code inside Main() and make sure you are getting the curretn ApplicationDomain details.

In my case I got below output

This Domain Application Base Is :E:\JAISH\ConsoleApplication1\ConsoleApplication
1\bin\Debug\

This Domain Friendly Name Is :ConsoleApplication1.vshost.exe

Pess any key to continue...

After above output confirmation close the application and dow below steps to prepare an assembly which will run in another fresh new ApplicationDomain.

1. Change the project Rebug mode to Release mode and build the entire solution.
2. You can see a "Release" folder under "bin". Take that "Release" folder and copy to C:\ and rename it as "AppDomainTest".
3. Insert below additional lines to Main() which is for creating a new ApplicationDomain and programmetically running "ConsoleApplication1.exe".
 
 if(AppDomain.CurrentDomain.FriendlyName.Equals("JaishDomain"))
            {
               return;
            }
            // Set up the AppDomainSetup
            AppDomainSetup setup = new AppDomainSetup();
            setup.ApplicationBase = @"C:\AppDomainTest";
            setup.ConfigurationFile = string.Empty;

            // Set up the Evidence
            Evidence baseEvidence = AppDomain.CurrentDomain.Evidence;
            Evidence evidence = new Evidence(baseEvidence);

            // Create the AppDomain     
            AppDomain newDomain = AppDomain.CreateDomain("JaishDomain", evidence, setup);
            newDomain.ExecuteAssembly("ConsoleApplication1.exe");


4. Make your project setting again to "Debug" from "Release" and run the application.

1st time you will same out put as you have already see. Once you press the enter the code to create new ApplicationDomain will get executed and will launch the assembly "ConsoleApplication1.exe". This time your Main() will agian fire, but under the new ApplicationDomain. You can see the difference through the output which would be like below

This Domain Application Base Is :C:\AppDomainTest
This Domain Friendly Name Is :JaishDomain
Pess any key to continue...

I am pasting the full Main() here. Don't forget to follow the 1 to 4 steps mentioned in above section to get the correct output 

 static void Main(string[] args)
        {
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(string.Format("This Domain Application Base Is :{0}" + Environment.NewLine + "This Domain Friendly Name Is :{1}", AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain.FriendlyName));
            Console.WriteLine(Environment.NewLine + "Pess any key to continue...");
            Console.ReadKey();

            if(AppDomain.CurrentDomain.FriendlyName.Equals("JaishDomain"))
            {
               return;
            }
            // Set up the AppDomainSetup
            AppDomainSetup setup = new AppDomainSetup();
            setup.ApplicationBase = @"C:\AppDomainTest";
            setup.ConfigurationFile = string.Empty;

            // Set up the Evidence
            Evidence baseEvidence = AppDomain.CurrentDomain.Evidence;
            Evidence evidence = new Evidence(baseEvidence);

            // Create the AppDomain     
            AppDomain newDomain = AppDomain.CreateDomain("JaishDomain", evidence, setup);
            newDomain.ExecuteAssembly("ConsoleApplication1.exe");
        }


 
Ebook Download
View all
Learn
View all