What is an AppDomain in .NET?

AppDomainToday I will try explain frequent questions which appear on StackOverflow. We will talk about Application Domain that is provided within .NET framework. In OS we have similar concepts like threads and processes.

AppDomain is really similar to process – provides a layer of isolation for applications that are not related with themselves. AppDomain like a process cannot get access to variables from another AppDomain.

Two different processes generally have two different memory spaces. Address of variable in process A, indicate to another place in memory than address of the same variable in process B. This is the reason why launching a new process is time-consuming – forcing allocation some of resources (e.g. memory).

The question is what is the difference between AppDomain and process? We know that layer of isolation is similar to process. The difference is that we can have within one process a few of AppDomains. The another difference is that AppDomain is faster than process and launch it requires allocation less amount of resources. Libraries once loaded in process cannot be removed. In use of AppDomain we can skip this problem: we can create a new AppDomain, load the libraries, then execute the code and and after all we can remove AppDomain – simple? I think yes.

So let’s go to summary. AppDomain is useful for:

  • plugins – you can unload app-domains (you can’t unload assemblies) e.g. you can  load dynamically generated DLLs and unload them
  • isolation – you can run different versions of assemblies e.g. it allow you to set different configuration files
  • security – you can run a set of code with specific trust levels e.g. in server application you can execute each request in separate isolated environment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s