Appdomain programming in. net

Source: Internet
Author: User
As we know, a process is a mechanism used by the operating system to isolate a large number of running applications. Before. net, each application is loaded into a separate process and a private virtual memory is specified for the process. The process cannot directly access the physical memory. The operating system maps the virtual memory to a region of the physical memory or I/O device through other processing methods, and the physical memory does not overlap, this makes it impossible for a process to access the memory allocated to another process. Correspondingly, applications running in this process cannot write to the memory of another application, which ensures that any code that fails to execute will not damage applications other than its address space. Under this mechanism, processes serve as an independent and secure boundary between applications, greatly improving operational security.

The disadvantage of the process is that it reduces the performance. Many processes that work together need to communicate with each other, but the process cannot share any memory. You cannot use any meaningful way to transmit the memory pointer from one process to another. In addition, you cannot directly call between two processes. Instead, you must use the proxy, which provides a certain degree of indirect. Although dynamic Connection Library dll allows all components to run in the same space, performance can be improved to some extent, but these components affect each other, A component error is very likely to cause the entire application to crash. "DLL hell" makes it hard for many applications to avoid.

Application domain (appdomain)

In. net, an application has a new boundary: The application domain (hereinafter referred to as the domain ). It is a virtual border used to isolate applications. This isolation is necessary to prohibit code that should not be interacted .. . NET applications are isolated at the domain level. Applications in one domain cannot directly access the code and data in another domain. This isolation allows all objects created within an application to be created in one domain, ensuring that the code running in one domain in the same process does not affect applications in other domains, this greatly improves operation security.

. Net structure, because the common language runtime can verify whether the code is type-safe 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 additional overhead for inter-process calls or switching. This method breaks down any process into multiple domains and allows multiple applications to run in the same process. Each domain corresponds to an application, each thread is running in a special domain. If different executable files run in the same process space, they can easily share data or directly access each other's data. This type of code is safe when running the same process but different types of security code in the domain are the same. The ability to run multiple applications within a process significantly enhances the scalability of the server.

Inter-Domain Communication

Domain is an important improvement brought about by. net. It not only isolates many running applications, but also does not affect communication between them. Although the common language runtime prohibits direct calls between objects in different domains, we can copy these objects or access these objects through a proxy. If the previous method is used, the object is called locally. That is, the caller and the referenced object are in the same domain. If an object is accessed through a proxy, the caller and the referenced object are in different domains, and the call to this object is considered as a remote call, this situation is roughly the same as the call between two processes or the call structure between two computers. In this case, the metadata of the referenced object can be used for both domains, so that the. NET instant compilation JIT can be correctly executed.

Relations between domains and threads

In. Net, a thread is the operating system structure used by the Common Language Runtime Library to execute code. During runtime, all managed code is loaded into a domain and run by a specific operating system thread. However, there is no one-to-one correspondence between the domain and the thread. At any given time, more than one thread can be executed in a single domain, and the specific thread is not limited to a single domain. That is to say, a thread can span the domain boundary without creating a thread for each domain. Of course, at the specified time, each line can only be executed in one domain. The Runtime Library tracks which threads are running in all domains. By calling the thread. getdomain method of the. NET class library, you can also determine the domain of the thread being executed.

Domain creation

As the isolation unit of the Common Language Runtime Library, the domain is created and run in the process .. In the. NET structure, the runtime host (also known as the runtime host) is an application responsible for loading the runtime into processes and Executing User code and hosting code in the domain. The runtime host includes ASP.. net, Internet Explorer, windows, and other Shell programs are responsible for creating processes and default domains, such as ASP.. Net creates a domain for each web application running on the Web server. The Browser Internet Explorer creates a domain for the Control to run.

For most applications, you do not have to create the corresponding domain. Each time the CLR initializes a process, it will create a default domain and run the process under this default domain. However, the default domain cannot be detached by any system call. The domain can be destroyed only after the process is detached. If code is directly programmed or run in the default domain, and the domain code crashes for some reason, the risk that the entire service will crash.

Therefore, for different applications, you should create and configure the corresponding domain and load the appropriate assembly .. NET provides a wide range of class libraries. Among them, the appdomain class is a programming interface of the domain, and a large number of (heavy load) methods can complete the following tasks:

· Create a domain

· Load the Assembly and type in the domain

· Program set and thread in enumeration Field

· Uninstall a domain

When creating a new domain, use the static method createdomain of the appdomain class. You can name the domain and reference the domain by the name. The following example statement creates a new domain 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 attribute friendlyname indicates the domain friendly name, which is formed by removing the directory path from the basic code of the Assembly. For example, if the Assembly named "D:/myappdomain/myassembly.exe" is loaded to the default domain, the friendly name of the domain is "myassembly.exe ".

Generally, the parameter of the domain is set before the domain is created. This can be done through appdomainsetup. The applicationbase attribute of this class defines the root directory of the application. The appdomainsetup class also has an extremely important attribute variable loaderoptimizzation, which can be multidomain, multidomainhost, signledomain, etc, used to specify the category of the loaded assembly (shared assembly or domain-specific assembly). For example, the following statement sets the Assembly as a domain-specific assembly:


     
      appDomainSetup.LoaderOptimization=LoaderOptimizatiion.SigleDomain;
     

The preceding two operations include setting parameters and creating two steps. The statement example is as follows:


     
      
Appdomainsetup = new appdomainsetup (); // sets appdomainsetup for the instantiated domain. loaderoptimization = loaderoptimization. singledomain; // specify the domain category appdoman ad = appdomain. createdomain (domainname, appdomainsetup); // create a domain... // The application runs the code here... appdomain. unload (AD); // unload the domain
     

Uninstall domain

When the domain is used up, you can use the appdomain class unload () Static Method to uninstall it. To uninstall the running managed code in a process, you can only uninstall the domain where the code is running, but not the independent assembly or type. The unload method closes the specified domain normally. At this time, all the Assembly loaded into the domain will be removed and cannot be used again. However, if the assembly in the domain is non-specific to the domain (the domain-Independent Assembly, that is, the shared assembly), the Assembly data will be kept in the memory until the entire process is closed. In addition to disabling the entire process, there is no mechanism to uninstall such an assembly. Because a process can contain multiple domains, a domain can be detached without stopping the entire process. Uninstalling unnecessary code in this way can reduce memory usage and greatly improve the scalability of applications. In addition, because the thread does not correspond to the domain one by one, when there is an active thread in the domain, calling the appdomain. Unload method may not uninstall the domain and cause exceptions.

Load the assembly in the domain

It is not difficult to see from the above discussion: to run an application, you must first load the Assembly (compiled under. net, including the Il intermediate language, metadata, and list) into the domain. In addition, multiple assemblies can be loaded in one domain. By default, the common language runtime automatically loads an assembly to a domain that contains the code that references the assembly. In this way, the code and data of the Assembly are independent of the application that uses the assembly.

One of the advantages of creating a domain on your own is that you can specify how to load the assembly. There are two ways to load an assembly in the domain:

1. Load the current Assembly into a separate domain. The same Assembly may have multiple copies;

2. Load the assembly in the form of a non-domain-specific, so that an assembly can be shared among multiple domains;

These two methods focus on both security and performance, and need to be weighed based on the actual situation. Specifically, in the. NET Framework, the system. reflection. Assembly class provides the following static methods to load the assembly to the domain:

· Load:


     
      
Assembly sampleassembly ;... Sampleassembly = assembly. Load ("system. Data"); // load the Assembly according to the type
     

· Loadfrom:


     
      
Assembly sampleassembly ;... Sampleassembly = assembly. loadfrom ("C: // sample. Assembly. dll"); // load according to the existing Assembly name
     


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.