What is a .NET application domain?

Luke picture Luke · Jul 7, 2009 · Viewed 47.1k times · Source

In particular, what are the implications of running code in two different application domains?

How is data normally passed across the application domain boundary? Is it the same as passing data across the process boundary? I'm curious to know more about this abstraction and what it is useful for.

EDIT: Good existing coverage of the AppDomain class in general at I don't understand Application Domains

Answer

Reed Copsey picture Reed Copsey · Jul 7, 2009

An AppDomain basically provides an isolated region in which code runs inside of a process.

An easy way to think of it is almost like a lighter-weight process sitting inside of your main process. Each AppDomain exists within a process in complete isolation, which allows you to run code safely (it can be unloaded without tearing down the whole process if needed), with separate security, etc.

As to your specifics - if you run code in 2 different AppDomains within a process, the code will run in isolation. Any communication between the AppDomains will get either serialized or handled via MarshallByRefObject. It behaves very much like using remoting in this regard. This provides a huge amount of security - you can run code that you don't trust, and if it does something wrong, it will not affect you.

There are many more details in MSDN's description of Application Domains.