CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Raymond Lewallen

Framework Design, Agile Coach, President Oklahoma City Developers Group, Microsoft MVP C#, TDD, Continuous Integration, Patterns and Practices, Domain Driven Design, Speaker, VB.Net, C# and Sql Server

What is an application domain - an explanation for .Net beginners

An application domain is the CLR equivalent of an operation system's process. An application domain is used to isolate applications from one another. This is the same way an operating system process works. The separation is required so that applications do not affect one another. This separation 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 addess space.

However, a CLR application domain is seperate from a process. It is contained within an operating system process. A single CLR operating system process can contain multiple application domains. There are some major pluses to having application domains within a single process.

  • Lower system cost - many application domains can be contained within a single sytem process.
  • The application in an application domain can be stopped without affecting the state of another application domain running in the same process.
  • A fault or exception in on application domain will not affect other application domains or crash the entire process that hosts the application domains.
  • Configuration information is part of an application domains scope, not the scope of the process.
  • Each application domain can have different security access levels assigned to them, all within a single process.
  • Code in one application domain cannot directly access code in another application domain. (* see below)

So you see, the CLR is like a mini-operating system. It runs a single process that contains a bunch of sub-process, or application domains.

* Direct communication cannot be acheived across application domains. Application domains can still talk to eachother by passing objects via marshalling by value (unbound objects), marshalling by reference through a proxy (AppDomain-bound objects). There is a third type of object called a context-bound object which can be marshalled by reference across domains and also within the context of its own application domain. Because of the verifiable type-safety of managed code, the CLR can provide fault isolation between domains at a much lower cost than an operating system process can. The static type verification used for isolation does not require the same process switches or hardware ring transitions that an operating system process requires.



Comments

John Papa said:

Raymond,

I've never had cause to use App Domains (mostly through ignorance I assume). I understand how they work but when I try to find a place to put it to practical use, well, I can't think of a practical one. I am sure there are some that I just haven't run across or thought of. Can you elaborate?
# April 3, 2005 9:35 PM

Sahil Malik said:

Man I love your blog. Seriously, the love and effort you put in your posts to really write meaningful posts useful to both a beginner and an expert makes it an utmost heartfelt pleasure to read them.
# April 3, 2005 11:24 PM

Geoff Appleby said:

Dude, cool. I've been meaning to research into appdomains for a very long time now, and just never got around to it. One question: Given that calls from one appdomain to another are marshalled, what's the cost involved in the calls?

Now that you've done a beginner post (of which the contents, while well written and very appreciated :) move on to the intermediate and advanced levels where my geekiness will thrive *grin* Seriously, the only thing that's stopped me playing is my lack of desire to take the time yet to find some examples. If you could do that, it's be sweet.
# April 4, 2005 3:52 AM

Geoff Appleby said:

Hey, don't apologise. Just do better next time!!! *grin* Just kidding :)
# April 4, 2005 6:20 AM

Raymond Lewallen said:

Sahil, thanks. I'm glad there are people out there who enjoy them. I pretty much leave the code and samples stuff up to David, he's so good at it ;)

John and Geoff, I'll do an intermediate level post on AppDomains with some elaborations on where you might want to use them and communicating across them. John, I've never created an AppDomain either for my own purposes. You really only want to create one in order to get the isolation and security boundries needed for executing some untrusted managed code. Jeffery Palermo has a post talking about how he runs a trace listener in a appdomain to contain memory usage and state here: http://dotnetjunkies.com/WebLog/jpalermo/archive/2005/02/22/56163.aspx

Geoff, to dig in deeper while I put up another post, you might want to check out the System.MarshalByRefObject and its derivatives. You also might want to check out remoting, which is all about communicating across app domains.

Also, for those of you seeking more information on creating your own application domains, check out the System.AppDomain class, which is derived from System.MarshalByRefObject.

My local .Net Developers meeting is this evening, so I probably can't get the next post done for another day or so, sorry.
# April 4, 2005 6:24 AM

Patrick Steele's .NET Blog said:

# April 6, 2005 8:00 PM

Sandeep said:

when does a appDomain Crash? I have a case where i create a number of appDomains and have to capture when any appDomain crash..
# August 8, 2006 8:51 AM

darshan said:

I hav que  .....when ll application crashs n ll it affect other application .what may  be the  reason behind it?

# September 14, 2006 12:46 AM

TrackBack said:

# March 27, 2007 9:13 PM

Saurabh Shah said:

Very much helful tutorial

# May 26, 2007 5:32 AM

Nickel said:

Very useful

# August 9, 2007 5:10 AM

prashant said:

Nice

thank you.

# November 1, 2007 2:11 AM

Ruju said:

Good one!!! I was not aware of adv. of app. domain. Thanx

# December 6, 2007 2:22 AM

Naresh Thandu said:

very good
# February 4, 2008 9:02 AM

Vishak said:

This tutorial about the Application Domain is very useful. Got an idea about it, after reading this article I guss any onw can dig into it.

# April 17, 2008 1:04 AM

Robert Sharp said:

I've actually got a problem with child AppDomains causing the hosting process to crash. All code in the hosted AppDomains that I have is contained in a giant Try...Catch block to avoid this, and for the most part it works beautifully. But there's one particular Service (that's what we call the children of the Host) that Interops with Outlook (Ok, I haven't seen the code that does it, I'm just assuming it uses Interop), and if Outlook happens to run into trouble (in the middle of the night when the Exchange server is down), it crashes the Host, so we lose all our Services along with it. Luckily I added session saving to the Host, to keep track of what was loaded in the event of a crash or power outage, but it's still annoying that the "uncrashable" crashes. Any tips? I mean, I do need to get a look at the code that uses Outlook... it was made by a slightly amature programmer, after all... I just would have thought the AppDomain structure would prevent it from being a problem.

# April 18, 2008 1:08 PM

kate said:

more information should be there..dude this is known by all..

# April 30, 2008 5:58 AM

Patent management » Application. Generally a, Application Domain said:

Pingback from  Patent management » Application. Generally a, Application Domain

# May 8, 2008 1:13 PM

Najeeb said:

Hi friend... Its a wonderful artle.. It really helped me a lot. Thanks..

# June 18, 2008 5:37 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Raymond Lewallen

Working primarily in the public sector during his career, Raymond has designed and built several high profile enterprise level applications for all levels of the government. Raymond now works as a solutions architect for EMC. Raymond is an agile coach, Microsoft MVP C# and also president of the Oklahoma City Developers Group and Oklahoma Agile Developers Group. Raymond spends a lot of his time learning and teaching such things as Test Driven Development, Domain Driven Design, Design Patterns and Extreme Programming practices and principles, to name a few. Raymond is also an advocate of Alt.Net. Raymond is primarily a framework guy, so don't ask him anything about UI :) Check out Devlicio.us!