Application Pools and Application Domains in C#

Application Pools and Application Domains are two different concepts, but having a good understanding of these concepts is imperative to build secure web applications. This article presents a deep dive into these concepts using the C# programming language.

What is an Application Pool?

Application pools comprise one or more worker processes that share identical configuration and application boundaries. An application pool consists of a collection of URLs served by a single worker process or a group of worker processes. Applications that execute on one application pool are not affected by other applications running on other application pools.

They enable you to isolate applications from one another, even if they reside on the same server and share identical configuration and application boundaries. This level of isolation provides better availability, security, performance, and reliability. This degree of isolation establishes the required protective barrier and guarantees the safety and security of your application.

It is possible to have one worker process per application or several applications sharing the same worker process. Both have their pros and cons. If you use the same worker process for multiple applications, it would be easy to make configuration changes. However, if many applications use the same worker process and if the worker process fails, all applications sharing the worker process will crash. On the contrary, if each application runs in its worker process, failure of one of the applications will not affect the other applications in any way.

Read more C# programming language tutorials.

How to Create and Configure an Application Pool

To begin, ensure that IIS is installed on your computer. If not, follow the steps outlined below to install IIS on your computer:

  • Launch Control Panel
  • Select “Programs and Features” from Control Panel Home
  • Click on “Turn Windows features on or off
  • Scroll down to the Internet Information Services checkbox

How to Install IIS

  • Check all options you would want to be installed
  • Click OK to start the installation

Windows will now download the necessary files to proceed with the installation.

To create an application pool, follow the steps shown below:

  • Launch IIS Manager
  • Select Application Pools
  • Right-click and select Add Application Pool…
  • Specify a name for your application pool
  • Select the CLR version
  • Select the managed pipeline mode
  • Ensure that the checkbox “Start application pool immediately” is checked
  • Click OK

Application Pools and Application Domains

The Classic and Integrated Pipeline Modes

Note that there are two pipeline modes: classic and integrated.

The Classic mode is only accessible with IIS6 and before. When an application pool is configured in Classic mode, IIS 7.0 processes requests in the same manner that IIS 6.0 does when the application pool is configured in worker process isolation mode.

If the application pool is configured in the Integrated mode, you can leverage the integrated request processing architecture provided by IIS and ASP.NET. It is a new mode in IIS7 that strongly integrates the IIS pipeline with the ASP.NET request pipeline.

What is an Application Domain?

An application domain is a lightweight process that provides a logical and physical isolation boundary for your applications running in the context of the CLR. It is a lightweight process that has its own set of data, code, and configuration settings.

Application domains enable multiple applications to coexist in the same process. The default application domain is created at the time when the CLR is initially loaded. If one application domain crashes or goes down, it does not interfere with the operation of another application domain.

An application domain with its own virtual address space is a logical barrier for code, security, reliability, and versioning. It should be noted that application domains are created by runtime hosts.

This isolation allows programs to remain in memory and execute in separate process boundaries. This means that two threads within the same application domain may communicate, but two threads inside different application domains cannot. You can run several .NET applications in a single process by loading these applications in different application domains.

What are Runtime Hosts?

The Common Language Runtime supports a wide range of applications, including Web applications, WPF applications, and so on. It should be noted that you need a runtime host to start any of these applications. The runtime host is responsible for loading the runtime into a process and creating application domains.

Why Are Application Domains Needed?

The CLR ensures that code pertaining to one application is not allowed to access the code or resources belonging to another application. This is accomplished by ensuring that code inside the managed environment undergoes a verification procedure to maintain type safety. Application domains assist the CLR in providing the necessary amount of isolation. Several applications can run inside the context of a single process without incurring significant performance costs, hence increasing scalability.

An application domain is a versatile and secure unit of processing used by the CLR to isolate applications running in the context of the IIS. You may execute several application domains in a single process with the same degree of isolation as separate processes, but without the added cost of performing cross-process calls or switching between processes. Neither do you need to switch between processes nor make cross-process calls. The ability to execute numerous applications in the same process significantly improves the scalability of the server.

Application Domains vs Processes

An application domain is a lightweight process suitable for situations that require isolation without the high cost of running an app within a process. In other words, it provides a level of isolation inside the CLR allowing multiple applications to co-exist in a single process. While a process is created and managed by the operating system, an application domain is created by the CLR.

A process is a program in execution identified by its Process Control Block (PCB). Code belonging to one process doesn’t have direct access to the code in another. Like an Application Domain, a process can’t access the code or data of other processes. You can run multiple applications in one process by loading them into different application domains.

Benefits of Application Isolation

Isolating applications help in securing the applications that run within the context of the CLR. Here are the benefits of application isolation at a glance:

  • Code running in one application is not permitted to directly access the code or resources of another application.
  • Faults in one application will not impact the other applications running in the context of the CLR.
  • You can stop individual applications without having to terminate the entire process.

Programming Application Domains in C#

You can use the following C# code snippet to retrieve information about the current domain.

AppDomain applicationDomain = AppDomain.CurrentDomain;
Console.WriteLine("Application Domain Name : {0}", AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("Application Domain Id : {0}", AppDomain.CurrentDomain.Id);

The AppDomain class pertaining to the System namespace can be used to create app domains. It contains a set of overloaded static methods that can be used to create an app domain.

You can use the following C# code example to create an app domain and then load and execute an assembly in it:

AppDomain applicationDomain = System.AppDomain.CreateDomain("MyAppDomain");
applicationDomain.ExecuteAssembly(@"D:\MyApp.exe");

You can use the Unload method of the AppDomain class to unload an app domain as shown in the code snippet given below:

AppDomain.Unload(applicationDomain);

Here’s the complete source code for your reference:

using System;
namespace AppDomainDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            AppDomain applicationDomain = 
            System.AppDomain.CreateDomain("MyAppDomain");   
         applicationDomain.ExecuteAssembly(@"D:\MyApp.exe");
            Console.WriteLine("Press any key to unload the 
            application domain...");
            Console.ReadKey();
            AppDomain.Unload(applicationDomain);
        }
    }
}

More by Author

Must Read