SIGN UP MEMBER LOGIN:    
ARTICLE

AppDomain concept in ASP.Net

Posted by Rajendra Kumar Yerra Articles | Current Affairs July 12, 2007
This article explains about the concept of an Application Domain which is shortly known as AppDomain.
Reader Level:

Asp.Net introduces the concept of an Application Domain which is shortly known as AppDomain. It can be considered as a Lightweight process which is both a container and boundary. The .NET runtime uses an AppDomain as a container for code and data, just like the operating system uses a process as a container for code and data. As the operating system uses a process to isolate misbehaving code, the .NET runtime uses an AppDomain to isolate code inside of a secure boundary.

The CLR can allow the multiple .Net applications to be run in a single AppDomain.

The CLR isolates each application domain from all other application domains and prevents the configuration, security, or stability of a running .NET applications from affecting other applications.An AppDomain can be destroyed without effecting the other Appdomains in the process.
 
Mulitple Appdomains can exist in Win32 process. As we discussed the main aim of AppDomain is to isolate applications from each other and the process is same as the working of operating system process. This isolation is achieved by making sure than any given unique virtual address space runs exactly one application and scopes the resources for the process or application domain using that address space.

Win32 processes provide isolation by having distinct memory addresses. The .Net runtime enforces AppDomain isolation by keeping control over the use of memory. All memory in the App domain is managed by the run time so the runtime can ensure that AppDomains Do not access each others memory.

How to create AppDomain

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
 
AppDomains are created using the CreateDomain method. AppDomain instances are used to load and execute assemblies (Assembly). When an AppDomain is no longer in use, it can be unloaded.

public class MyAppDomain : MarshalByRefObject
{
    public string GetInfo()
    {
        return AppDomain.CurrentDomain.FriendlyName;
    }
} 

public class MyApp

{

    public static void Main()

    {

        AppDomain apd = AppDomain.CreateDomain("Rajendrs Domain");

        MyAppDomain apdinfo = (MyAppDomain)apd.CreateInstanceAndUnwrap (Assembly.GetCallingAssembly().GetName().Name, "MyAppDomain");

        Console.WriteLine("Application Name = " + apdinfo.GetInfo());

    }

}

 

The AppDomain class implements a set of events that enable applications to respond when an assembly is loaded, when an application domain will be unloaded, or when an unhandled exception is thrown.

Advantages

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

  1. 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.

Summary

This article explained about the Appdomain concept in Asp.net.

Login to add your contents and source code to this article
share this article :
post comment
 

If you have 10 ASP.Net applications on single (IIS) web server, all application can run with in same process. How ever ASP.Net creates a separate Application domain for each ASP.Net application. As per my knowledge there is nothing to do with website users with application domain.

Posted by sachin tekriwal Jul 08, 2011

Application domains are implemented in the .Net Framework using the System.AppDomain class. To use an application domain, create an instance of the of the AppDomain class.

Posted by sachin tekriwal Jul 08, 2011

Repested Hope you are fit as a fiddle I was surfing regarding the appdomain. I found your article very awesome to deliver the concept. I have some questions and i hope that i will get the positive response. 1) Does AppDomain and Application domain are the same thing. 2) In the prespective of one website running in IIS, if currently 50 users are connected to the website, does it create one master appdomain for the global and static members of the website and then for each user child appdomains with in the master appdomain. Thanks in Advace Regards TanzeelurRehman

Posted by TanzeelurRehman Qureshi Jun 19, 2011

Very Good article...Very well written....Keep posting such articles....

Posted by Amit Oct 13, 2010

Hi Rajendra,

I found this article very helpful and interesting.

Thanks
Uday Gaikwad, Pune
uday.gaikwad@hotmail.com

Posted by Uday Gaikwad Aug 30, 2010
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor