joydip_kanjilal
Contributor

Working with application domains in .Net

opinion
Jan 04, 20164 mins
Software Development

Leverage application domains to provide a level of isolation inside the managed environment so as to enable multiple applications to run in a single process

An application domain is a lightweight process and acts as a logical boundary that provides an isolation boundary for code, application security, reliability and versioning.

Process boundaries have been in use for a long time to isolate applications that execute in the same system. Note that a process is the running instance of a program. This isolation helps to have applications reside in the memory and execute in different process boundaries. So, two threads in the same application domain can communicate with each other, but two threads that belong to two different application domains can’t.

A thread is the smallest unit of execution within a process.  You can have multiple application domains residing inside a single process and more than one thread inside an application domain. An application domain (commonly called AppDomains) is a logical unit of isolation that enables you to execute multiple applications within the same process while at the same time ensuring that crash of a particular application domain doesn’t affect the functioning of another application domain.

Why do we need application domains?

The common language runtime environment ensures that code running inside one application cannot access the code or resources of another application running within the context of the managed environment. How is this accomplished? Managed code or code that executes inside of the managed environment needs to pass through a verification process. This verification is done the CLR (common language runtime) to ensure type safety. Application domains help the CLR to provide the required level of isolation so that several applications can execute within the context of a single process sans much performance overhead to increase scalability.

The MSDN states: “Application domains provide a more secure and versatile unit of processing that the common language runtime can use to provide isolation between applications. You can run several application domains in a single process with the same level of isolation that would exist in separate processes, but without incurring the additional overhead of making cross-process calls or switching between processes.”

Creating application domains programmatically

Before we create a new application domain programmatically, let’s explore how we can retrieve the metadata of the current application domain and executing assembly using C#. The following code snippet illustrates how you can display the application domain and the assembly names of the currently executing assembly.

using System;

using System.Threading;

using System.Reflection;

namespace ApplicationDomains

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine(Thread.GetDomain().FriendlyName);

            Console.WriteLine(Assembly.GetEntryAssembly().FullName);

            Console.ReadLine();

        }

    }

}

Similarly, you can retrieve the metadata information of the host and the child domains using the static members of the AppDomain class.

using System;

namespace ApplicationDomains

{

    class Program

    {

        static void Main(string[] args)

        {

            AppDomain childApplicationDomain = AppDomain.CreateDomain("IDGApplicationDomain");

            Console.WriteLine("The host domain name is:  " + AppDomain.CurrentDomain.FriendlyName);

            Console.WriteLine("The host domain id is:  " + AppDomain.CurrentDomain.Id.ToString());

            Console.WriteLine("The child domain name is:  " + childApplicationDomain.FriendlyName);

            Console.WriteLine("The child domain id is:  " + childApplicationDomain.Id.ToString());

            Console.ReadKey();         

        }

    }

}

You can create a new application domain using one of the overloaded CreateDomain methods of the System.AppDomain class. Note that all of these methods are static hence you can invoke them sans the need of instantiating the AppDomain class. Here’s the list of the overloaded CreateDomain methods of the System.AppDomain class.

public static AppDomain CreateDomain(String appDomainName)

public static AppDomain CreateDomain(String appDomainName, Evidence securityInformation)

public static AppDomain CreateDomain(String appDomainName,

   Evidence securityInformation, AppDomainSetup appDomainSetupInformation)

public static AppDomain CreateDomain(String name,

   Evidence securityInformation, String appBasePath, String appRelativeSearchPath,

   bool shadowCopyFiles)

You can create an application domain using any of these overloaded CreateDomain methods — you can just pass the name of the application domain you would like to create as a parameter to this method. You can also pass the security policies if you want to as an additional parameter. The ExecuteAssembly method is used to load and execute an assembly in an application domain.

The following code listing shows how you can create a new application domain and then load and execute an assembly inside the newly created application domain.

using System;

namespace ApplicationDomains

{

    class Program

    {

        static void Main(string[] args)

        {

            AppDomain applicationDomain = System.AppDomain.CreateDomain("IDGAppDomain");

            applicationDomain.ExecuteAssembly(@"D:ProjectsTestCode.exe");

 Console.WriteLine("Press any key to unload the application domain...");

            Console.ReadKey();

            System.AppDomain.Unload(applicationDomain);

        }

    }

}

When the above program is executed, a new application domain named “IDGAppDomain” will be created and then an assembly (named “TestCode.exe”) loaded into the application domain and executed. The application domain that has been created will be unloaded once a key is pressed.

joydip_kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author