Understand Application domain in C# .NET

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 39959 red flag

How to use and create application domain in C# .NET

Understand Application domain in C# .NET

In this article we will discuss about Application domain in C#.NET. I hope you have basic understanding of .NET and its architecture.  Application domain (other name appDomain) is nothing but one logical region where .NET runtime runs and execute code.  It provides security and isolation for executing managed code.

We can use application domain to isolate any task that might bring down process. From application domain we can unload any task without affecting the process. We can also use application domains to isolate tasks that should not share data. In this way application domain gives security to different process.

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

Multiple application domains can exist in one Win32 process. As we have discussed earlier that the primary goal of application domain is to separate or isolate process and it is very similar with different processes in one operating system. The  .NET runtime force application domain isolation by keeping control over memory.

Get application domain name and assembly name

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Reflection;
 
namespace TestDomain
{
    class Program
        {
            public static void Main(String[] args)
            {
                string callingDomainName = Thread.GetDomain().FriendlyName;
                Console.WriteLine(callingDomainName);
 
                //Get and display the full name of the EXE assembly.
                string exeAssembly = Assembly.GetEntryAssembly().FullName;
                Console.WriteLine(exeAssembly);
 
                Console.ReadLine();
            }
        }
}

Here is sample output


Create application domain

In this example we will see how to create one application domain and how to load and unload assembly in this application domain. Have a look on below code.

using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Reflection;
 
namespace TestDomain
{
    class Program
        {
            public static void Main(String[] args)
            {
                //Create an new Application Domain:
                System.AppDomain newDomain = System.AppDomain.CreateDomain("NewApplicationDomain");
 
                //Load and execute an assembly:
                newDomain.ExecuteAssembly(@"C:\HelloWorld.exe");
 
                //Unload the application domain:
                System.AppDomain.Unload(newDomain);
                Console.ReadLine();
            }
        }
}

Let’s understand the example. We can create our own application domain from AppDomain Class. This class belongs to System namespace. We have to call CreateDomain() function of AppDomain class. Here we are passing application domain name as argument of CreateDomain() function.

To load and execute any assembly we have to call ExecuteAssembly() function. Here we are specifying path of HelloWorld.exe which is located in C drive of my system.

After executing assembly we can unload it by calling Unload() static function. We have to supply AppDomain object to unload any application domain.

This is output of above example.


Conclusion:-

In this article we have tried to understand what application domain is, what its advantage is and how to create our own application domain. Hope you have understood the concept.

 

 

 

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)