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. Related content feature What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks Resources Videos