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("\t 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\rahul\Documents\visual studio 2015\Projects\HelloWorld\HelloWorld\bin\Debug\HelloWorld.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: -

 

 

:: SHARE THIS POST ::

        
 

More C# Articles To Read Next:

 
Dec
01
2016

Delay signing in C# with example

CLICK TO READ FULL ARTICLE

Views - 194 |Category - C#

Nov
28
2016

Tuples in c# example step by step

CLICK TO READ FULL ARTICLE

Views - 354 |Category - C#

Nov
28
2016

C# Anonymous Types vs Tuples with an example

CLICK TO READ FULL ARTICLE

Views - 453 |Category - C#

Nov
22
2016

Anonymous Methods in C# with an example Step by Step

CLICK TO READ FULL ARTICLE

Views - 1464 |Category - C#

Oct
30
2014

Understand Monitor vs Mutex vs Semaphore vs SemaphoreSlim - Onlinebuff

CLICK TO READ FULL ARTICLE

Views - 13359 |Category - C#

 
Author: 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, Cell Phones and lots more. Follow me @ and twitter.
Leave a Comment......

All fields marked with * are mandatory

fnmkh