App Domain in C#.NET Step by Step using example

In this article let's understand about app domain in csharp(C#).NET step by step using simple example.

The logical container, without one container affecting the other. An AppDomain provides a layer of isolation within a process. It can be viewed as lightweight process which is both a container and boundary. They share many of the same characteristics of a process.

Multiple application domain can run a single process and there might be more than one thread in single application domain.

AppDomains are generally created by Hosts for example Internet Explorer and Asp.net. The following is an example to create instance of an object inside it and then executes one of the objects methods. This is the explicit way of creating AppDomain by .Net Applications.

App Domain Simple example in C#.NET

Create simple app domain application

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace applicarionDomain001
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ReadLine();
            Console.WriteLine("	 This is HelloWorld Application runing undre a Application Domain");
            Console.ReadKey();
        }
    }
}
хехехехееехехехех

output

Create application Domain

In this example, we will see how to create one application domain.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UseAPPDo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Read();
            AppDomain ad = AppDomain.CreateDomain("HelloWorld Domain");
            ad.ExecuteAssembly(@"C:Users
ahulDocumentsvisual studio 2015ProjectsHelloWorldHelloWorldinDebugHelloWorld.exe");
        }
    }
}

How to load in this application domain.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Policy;

namespace UseAPPDo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Read();
            //  //Create an new Application Domain:
            AppDomain ad = AppDomain.CreateDomain("HelloWorld Domain");
            //Load and execute an assembly:
            ad.ExecuteAssemblyByName("HelloWorld");
 
        }
    }
}

output

How to Unload assembly in this application domain

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Policy;

namespace UseAPPDo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Read();
            //  //Create an new Application Domain:
            AppDomain ad = AppDomain.CreateDomain("HelloWorld Domain");
            ////Load and execute an assembly:
            //ad.ExecuteAssemblyByName("HelloWorld");

            //Unload the application domain:
            AppDomain.Unload(ad);
        }
    }
}

Output

App Domain Advantages

A single CLR operating system process can contain multiple application domains. There are advantages to having application domains within a single process.

  1. In the lower system cost many application domains can be contained within a single system process.
  2. Each application domain can have different security access levels assigned to them, all within a single process.
  3. Code in one AppDomain cannot directly access code in another AppDomain.
  4. The application in an AppDomain can be stopped without affecting the state of another AppDomain running in the same process.
  5. An Exception in on AppDomain will not affect other AppDomains or crash the entire process that hosts the AppDomains.

If you are new to C# language and .NET platform below is a worth watching video covering learning fundamental with practical project: -

Author: Gurunatha Dogi

Gurunatha Dogi

Gurunatha Dogi is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server and lots more..... read more

Add a Comment