Analysis. Appdomain programming under NET

xiaoxiao2021-03-06  116

We know that the process is the mechanism for operating the system to isolate numerous applications that are running. in. Before Net, each application is loaded into a separate process and specifies private virtual memory for the process. The process cannot directly access physical memory, and the operating system maps these virtual memory to a certain area of ​​physical memory or IO devices through other processing, and these physical memory will not overlap, which determines a process that is impossible to access allocation. Give another process of memory. Accordingly, applications running in this process cannot be written to another application, which ensures that any error-performing code does not damage applications other than their address spaces. Under this mechanism, the process is a large and secure boundary between the application to a large extent to a large extent. The disadvantage of the process is to reduce performance. Many processes work together need to communicate with each other, but the process cannot share any memory, you can't use a memory pointer that passes from a process to another by any meaningful way. In addition, you cannot call directly between two processes. You must use it to use the agent, which provides a certain degree of indirectity. Although using dynamic connection library DLL allows all components to run in the same space to a certain extent, performance can be improved, but these components are influential, and one component's error will be extremely likely to lead to the crash of the entire application, "DLL Hell" is more Many applications are difficult to avoid.

Appdomain

in. In NET, the application has a new boundary: application domain (hereinafter referred to as domain). It is a virtual boundary for isolating the application. This isolation is necessary to prohibit the code that should not interact. . NET's application is isolated at the domain hierarchy, and the application in one domain cannot directly access the code and data in another domain. This isolation makes all objects created in an application range in one domain, ensuring that the code running within one domain in the same process does not affect applications in other domains, which greatly improves operational security. In the .NET structure, since the public language runtime can verify that the code is a type of security code, it can provide the same isolation level as the process boundary, and its performance overhead is much lower. You can run several domains in a single process without causing additional overhead between processes or switches. This method is to decompose any process into multiple domains, allowing multiple applications to run in the same process, and each domain corresponds to an application, each thread running is in a special domain. If different executables are running in the same process space, they can easily share data or directly access each other's data. This code is safe when running the same process, but the domain security code is safe. The ability to run multiple applications in a process has significantly enhanced the scalability of the server.

Domain communication

The domain is. An important improvement brought by NET, which not only isolates many of the running applications, but also does not affect the communication between him. Although the public language runtime is prohibited from being called between objects in different domains, we can copy these objects or access these objects through the proxy. If in the previous way, the call to the object is local call. That is, the caller and the referenced object are located in the same domain. If the call to the object, the caller and the referenced object is located in different domains, the call to the object is considered remote call, which is approximately the same as the call between the two processes or the call structure between the two computers. . At this time, the metadata that needs to be referenced is available for both domains so that .NET instantly compiles JIT to be executed correctly.

Domain and thread relationship

In .NET, the thread is an operating system constructor used by the public language runtime to perform code. At runtime, all managed code is loaded into a domain, running by a specific operating system thread. However, there is no correspondence between domain and threads. In any given time, more than one thread can be performed in a single domain, and the specific thread is not limited to a single domain. That is, the thread can span the domain boundary, not to create a new thread for each domain. Of course, at the specified time, each thread can only be executed in one domain. Running will track which threads in all domains are running. By calling. The THAAD.GETDOMAIN method of the NET class library, you can also determine the domain where the thread being executed is. Domain creation

As the isolation unit of the public language runtime, the domain creates and runs in the process. . In the NET structure, the runtime host (also called runtime host) is responsible for the running process and executes user code and hosting applications in the domain. Runtime host includes ASP. NET, browser Internet Explorer and Windows and other shells are responsible for creating processes and default domains, for example, ASP. NET creates a domain for a web application running on a web server. Browser Internet Explore creates a domain that runs controlled controlled controls. For most applications, you don't have to create a corresponding domain. Each time the CLR initializes a process, the default domain will be created, and the process is running under this default domain. However, the default domain cannot be uninstalled by any system call, which is only destroyed after the process is uninstalled. If you program or run the code directly under the default domain, the risk of crashing the entire service is made due to the code of a reason domain. Thus, for different applications, you should create and configure the appropriate domain and load the appropriate assembly. . Net provides a rich class library for this. Among them, the AppDomain class is a domain programming interface. Its a large (overload) method can complete the following tasks: • Creating a domain • Add assemblies in the domain and types and threads in the domain, unloading domains create new When the domain is used, the static method of the Appdomain class CreateDomain is used. You can name the domain and press this name to reference the domain. The following sample statement creates a new field and specifies the name Mydomain for it:

Appdomain mydomain = appdomain.createdomain ("mydomain");

Then you can query the name of the current domain and the name of the newly created subdomain:

String hostdomain = appdomain.currentdomain.friendlyname;

String childdomain = mydomain.friendlyname;

Here, the property FriendlyName is represented by the domain's friendly name, and the friendly name is formed by removing the directory path from the basic code of the assembly. For example, the file name "D: /myAppDomain/myassembly.exe" is loaded into the default domain, and the domain's friendly name is "Myassembly.exe". More generally, before creating domains, set the parameters of the domain, which can be done by class appdomainsetup. The ApplicationBase property defines the root directory of the application. There is also an extremely important attribute variable of the AppDomainSetup class. The value can be MultiDomain, MultiDomainHost and SignDomain, to specify the category of loaded assembly (shared assembly or domain) Dedicated assembly), for example, the following statement sets the assembly to a domain private assembly: AppDomainSetup.LoaderOptimization = loaderoptimizatiion.sigledge

Simple summary of the above two aspects, typical operations on the domain include: Setting parameters and create two steps, statement examples:

Appdomainsetup AppdomainSetup = New AppdomainSetup (); // Instantiation Domain Settings

Appdomainsetup.LoaderOptimization = loaderoptimization.singleDomain; // Specify domain categories

Appdoman ad = appdomain.createdomain (domainname, appdomainsetup); // Create a domain

. . .

// Applications run the code here

. . .

Appdomain.unload (ad); // Unloading domain

Unload domain

When using an outsomation, you can use the AppDomain unload () static method to uninstall it. To uninstall the managed code in the process, you can only uninstall the code when the code is run, without unloading a separate assembly or type, and UNLOAD methods will properly turn off the specified domain. At this time, all assemblies loaded into the domain will be removed and cannot be used again. However, if the assembly in the domain is non-specific (the domain independent assembly, that is, the shared assembly), the data set of the assembly will remain in the memory until the entire process is turned off. In addition to closing the entire process, there is no mechanism to uninstall such an assembly. Since a process allows a plurality of domains, a domain can be uninstalled without stopping the entire process. In this way, unloading the code that is no longer needed can reduce memory occupancy and greatly improve the scaling of applications. In addition, since the thread does not correspond to the domain, when there is an active thread in the domain, call the appdomain.unload method may not uninstall the domain and cause an exception.

Load assemblies in the domain

It is not difficult to see from the above discussion: To run the app, you must first load the assembly (.NET to be generated, including IL intermediate language, metadata, and list, etc.). And multiple assemblies can be loaded in one domain. By default, the public language runtime automatically loads an assembly to the domain that contains the code that references the assembly. Through this method, the code and data of the assembly are independent of the application using the assembly. One of the benefits of the domain to create a domain can specify how to load an assembly. There are two ways to load assemblies in the domain: 1. Pack the current assembly into a separate domain, and there may be multiple copies of the same assembly; 2. Load the assembly in the form of a domain, let one The assembly is shared between multiple domains; these two modes are each critical to security and performance, and they need to trade between the specific situations. Specifically, in. In the NET framework, the System.Reflection.Assembly class provides the following static method to load the assembly to the field: Load () Loads the assembly in the premise of the top assembly name: assembly samplessembly;

...

Samplessembly = askEMBLY.LOAD ("system.data"); // Load assembly according to type

· LoadFrom () Load assembly in the case of information such as a known assembly file name or path:

AskEMBLY SAMPLEASSEMBLY;

...

Samplessembly = askEMBLY.LOADFROM ("c: //sample.assembly.dll"); // loaded according to the existing program set name

转载请注明原文地址:https://www.9cbs.com/read-125827.html

New Post(0)