Appdomain and how to change the location of Web. config/APP. config

Source: Internet
Author: User


Concept:

Appdomain (ApplicationProgramIs a concept exclusive to the. NET Framework. It is Microsoft's own business that cannot find the appropriate reference concepts in other technical systems. Appdomain, which is an independent environment in which an application executes.CodeProvides isolation, uninstallation, and security boundaries.


AppdomainIndependence:

AppdomainYes is both a container and a boundary .. Net runtime uses appdomain as the container for code and data, just like an operating system process as the container for code and data. When the operating system uses a process to separate untidy code,. Net runtime uses an appdomain to separate the code within a security edge. Each appdomain is allocated a dedicated storage area (application domain local storage) by the. NET Framework ). Any object can access the local storage area of its current appdomain, which is shared by objects in the entire appdomain, it also includes the thread that enters the appdomain (the thread running on the same appdomain can communicate through this local storage ).


Relationship between processes and appdomain:

It is a one-to-many relationship, that is, a process can have multiple AppDomains, but the appdomain can only exist in a process. By default, if you do not create multiple AppDomains by yourself, an appdomain is automatically created after a process starts. Multiple AppDomains run in the same. Net process. The advantage is that they can reduce system consumption, and different AppDomains are isolated from each other, providing better independence and security.


Relationship between threads and appdomain:

There is no one-to-one association between application domains and threads. Multiple Threads can belong to one application domain. Although the given thread is not limited to one application domain, the thread is executed in one application domain at any given time. Thread execution can involve multiple AppDomains, but at a specific time, the thread only exists in one appdomain, And the thread can enter other AppDomains. An instance of an Assembly belongs to a specific appdomain, which is loaded within its own range, and corresponding objects are created according to the Assembly. Appdomain is the execution environment of the Assembly. At the same time, the Assembly acts as a static entity and can be loaded and executed by multiple AppDomains.


AppdomainTo create and uninstall:

Application domain by using
Createdomain
Method.AppdomainAn instance is used to load and execute an assembly ). When not used
AppdomainYou can detach it.

Appdomainsetup setup =   New Appdomainsetup ();
Setup. loaderoptimization = Loaderoptimization. singledomain;
Setup. applicationbase =   " (Some directory) " ;
Setup. configurationfile =   " (Some file) " ;

Appdomain = Appdomain. createdomain ( " Newdomain " , Null , Setup );
Domain. executeassembly ( " C: \ newdomain.exe " );
Appdomain. Unload (domain );

In those cases, you need to use appdomain:

In fact, in general, we do not need to care about appdomain. However, this concept appears in. net is not redundant and has its own reasons. What are the specific scenarios for using appdomain?

    1. The assembly to be isolated. For example, some code that is particularly prone to crash can be considered to run separately in a specific appdomain.
    2. If you need to divide Security Execution boundaries for your code for different security-level assemblies, consider creating different security-level codes separately in an appdomain with different security information set.
    3. In terms of performance, some assemblies may consume a large amount of resources. Although there is basically no resource consumption vulnerability in the hosting environment, there will always be a situation where intensive access at a specific time causes a large amount of resources to be consumed, in this case, you can create a separate appdomain and uninstall the appdomain when the resource consumption exceeds the critical point to meet the system operation requirements. In Asp.net, different AppDomains are used to provide support to prevent the crash of one application from affecting other Asp.net applications. At the same time, if the system that does not restart IIS and does not affect the service provision of Asp.net itself, an appdomain is detached and a new appdomain is started, ideally, the web system can be online for a long time (this was an expensive UNIX feature and was finally referred to by MS ).
    4. Different versions of the same application Assembly run simultaneously. This is a big problem in the com era. Now, through appdomain, we have implemented the implementation of two different assembly versions in one process, which can achieve good compatibility.
    5. Dynamically load some programs. For example, dynamically creating an appdomain to start a background monitoring program will not affect the running of the main process even if it crashes.

Change appdomain attributes:

 Use the currentdomain attribute of the appdomain class to check the attributes of the appdomain in which the code is running.

How to changeWeb. config
/APP. configLocation:

Web. config/
App. config/*. EXE. configAll are in the same directory of the application assembly by default,. NET 2.0
ConfigurationThis configuration file is very dependent. If we want to move it to another directory, for example"C: "Documents
And Settings "myusername" Application Data"", Without affecting. Net
2.0 ConfigurationWait for all the tasks that depend on this configuration file. How can this problem be solved? ConfigurableAppdomainProperties:

String Newconfigfilepath =   @" C: \ Documents ents and Settings \ myusername \ Application Data \ MyApp. config " ;

If (System. appdomain. currentdomain. setupinformation. configurationfile ! = Newconfigfilepath)
Appdomain. currentdomain. setdata ( " App_config_file " , Newconfigfilepath );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.