Document directory
- Introducing the application domain
- AppDomains and you
- Shadow Copies and restarts
- Master of your domain
In traditional development, we all know that an application corresponds to a process and specifies virtual memory for the process. The operating system maps the actual physical memory, it effectively maintains the security between processes. On the other hand, every process consumes a certain amount of system resources, reduces performance, and makes communication between processes troublesome.
In. Net, a new concept is introduced: application domain (appdomain ). It can be understood that many application domains can run in the same. Net process, which can reduce system consumption and isolate different domains to ensure security. In addition, communication between different domains in the same process is relatively simple.
When we launch the Notepad program in windows, the program executes inside of a container known as a process. we can launch multiple instances of notepad, and each instance will run in a dedicated process. using the Task Manager application, we can see a list of all processes currently executing in the system.
A process contains the executable code and data of a program inside Memory it has reserved from the operating system. there will be at least one thread executing instructions inside of the process, and in most cases there are multiple threads. if the program opens any files or other resources, those resources will belong to the process.
A process is also boundary. erroneous code inside of a process cannot permitted upt areas outside of the current process. it is easy to communicate inside of a process, but special techniques are required to communicate from one process to another. each process also runs under a specific security context which can dictate what the process can do on the machine and network.
A process is the smallest unit of isolation available on the Windows operating system. this coshould pose a problem for an ISP who wants to host hundreds of ASP. NET applications on a single server. the ISP will want to isolate each ASP. NET application to prevent one application from interfering with another company's application on the same server, but the relative cost of launching and executing a process for hundreds of applications may be prohibitive.
Introducing the application domain
. NET introduces the concept of an application domain, or appdomain. like a process, the appdomain is both a container and a 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,. net runtime uses an appdomain to isolate code inside of a secure boundary.
Note, however, that the application domain is not a secure boundary when the application runs with full trust. applications Running with full trust can execute native code and circumvent all security checks by. net runtime. ASP. net Applications run with full trust by default.
An appdomain belongs to only a single process, but single process can hold multiple AppDomains. an appdomain is relatively cheap to create (compared to a process), and has relatively less overhead to maintain than a process. for these reasons, an appdomain is a great solution for the ISP who is hosting hundreds of applications. each application can exist inside an isolated appdomain, and contains of these AppDomains can exist inside of a single process-a cost savings.
AppDomains and you
You 've created two ASP. NET applications on the same server, and have not done any special configuration. What is happening?
A single ASP. net Worker Process will host both of the ASP. NET applications. on Windows XP and Windows 2000 this process is named aspnet_wp.exe, and the process runs under the security context of the local ASPNET account. on Windows 2003 the worker process has the name w3wp.exe and runs under the network service account by default.
An object lives in one appdomain. each ASP. NET application will have it's own set of global variables: cache, application, and session objects are not shared. even though the code for both of the applications resides inside the same process, the unit of isolation is. net appdomain. if there are classes with shared or static members, and those classes exist in both applications, each appdomain will have it's own copy of the static fields-the data is not shared. the code and data for each application is safely isolated and inside of a boundary provided by the appdomain
In order to communicate or pass objects between AppDomains, you'll need to look at techniques in. Net for communication between SS boundaries, such as. Net remoting or web services.
Note again: the one caveat to the idea of an appdomain as a boundary is that ASP. net Applications will run with full trust by default. fully trusted code can execute native code, and native code can essentially have access to anything inside the process. you'll need to run applicationsPartial TrustTo restrict access to unmanged code and verify all managed code to secure AppDomains.
Shadow Copies and restarts
Once an assembly is loaded into an appdomain, there is no way to remove the Assembly from the appdomain. It is possible, however, to remove an appdomain from a process.
If you copy an updated DLL into an application's bin subdirectory, the ASP. net runtime recognizes there is new code to execute. since ASP. net cannot swap the DLL into the existing appdomain, it starts a new appdomain. the old application domain is "drain stopped", that is, existing requests are allowed to finish executing, and once they are all finished the appdomain can unload. the new appdomain starts with the new Code and begins taking all new requests.
Typically, when a DLL loads into a process, the process locks the DLL and you cannot overwrite the file on disk. however, AppDomains have a feature known as shadow copy that allows assemblies to remain unlocked and replaceable on disk.
The runtime initializes ASP. net with Shadow Copy enabled for the bin directory. the appdomain will copy any DLL it needs from the bin directory to a temporary location before locking and loading the DLL into memory. shadow Copy allows us to overwrite any DLL in the bin directory during an update without taking the web application offline.
Master of your domain
Application domains Replace the OS process as the unit of isolation. net code. an understanding of application domains will give you an idea of the work taking place behind the scenes of an ASP. NET application. using the currentdomain property of the appdomain class you can inspect properties about the appdomain your code is executing in, including the shadow copy settings we discussed in this article.