tags:

views:

1851

answers:

2

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 http://stackoverflow.com/questions/622516/i-dont-understand-appdomains

+6  A: 

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.

Reed Copsey
Can you clarify what you mean by "if it does something wrong"?
Luke
One example: If you have an unhandled thread in a threadpool, it will tear down the app domain. Normally, this will kill your process - which is dangerous if you're loading user-code or a plugin. Running in a separate appdomain means you can handle that much better - if the second AppDomain has to be torn down, you can handle that without tearing down your process.
Reed Copsey
+2  A: 

It is an isolation layer provided by the .NET runtime. As such, App domains live with in a process (1 process can have many app domains) and have their own virtual address space.

App domains are useful because:

  • They are less expensive than full processes
  • They are multithreaded
  • You can stop one without killing everything in the process
  • Segregation of resources/config/etc
  • Each app domain runs on its own security level
nikmd23