What is an application domain? (Zhuan)

Source: Internet
Author: User

 

I. Application domain introduction:

"Domain" refers to the scope, environment, and boundary. What is the application domain? The official explanation is as follows:

Provides some form of isolation between applications.

Application domains provide isolation boundaries for security, reliability, version control, and uninstallation of an assembly.The application domain is often created by the runtime host,

The runtime host directs the public Language Runtime Library before running the application.

The application domain is essentiallyIsolationThe purpose is to prevent the code running in the application from directly accessing the code or resources in other applications.

If you need to access objects in other applications, you can copy these objects or access these objects through a proxy.

2. Application domain and assembly

The relationship between the application domain and the Assembly.Before executing the Code contained in an assembly, the Assembly must be loaded into the application domain.

Running a common application will cause several assemblies to be loaded into an application domain.

The Assembly Loading Method determines whether the real-time (JIT) compiled code can be shared by multiple application domains in the process, and whether the Assembly can be

Detach from a process:

1: If the Assembly is loaded in a domain-specific manner, all application domains that share the same security authorization set can share the same JIT encoding.

Translate the code to reduce the memory required by the application. However, the Assembly can never be detached from the process.

2: If the Assembly is not loaded in the form of a non-domain-specific, it must be JIT compiled in each loaded application domain. However, you can detach an assembly from a process by detaching all application domains loaded by the Assembly.

Iii. Use of AppDomain class

The. NET Framework providesAppDomain class to create and detach domains and create various types of instances in the domains. What are the methods in this class,

The following is a brief introduction:

1: CreateDomain: application domain for innovation,

2: ExecuteAssembly or ExecuteAssemblyByName method: Execute the assembly in the application domain. This is an instance method, so it

It can be used to execute code in another application domain (You have reference to this domain:

3: CreateInstanceAndUnwrap: Create an instance of the specified type in the application domain and return a proxy. Use this method to avoid including

The created assembly type is loaded into the calling assembly.

4: Unload: The execution domain is shut down normally. Only running threads in the application domain are stopped or no longer running threads in the domain are uninstalled.

The application domain.

4. programming example:

The following example shows how to create an application domain and disable an application domain:

Using System;
Using System. Reflection;
Using System. Threading;
Class Example
{
Public static void Main ()
{
String callingDomainName = Thread. GetDomain (). FriendlyName;

String exeAssembly = Assembly. GetEntryAssembly (). FullName;
// Set a new application domain
AppDomainSetup a = new AppDomainSetup ();
A. ApplicationBase =
System. Environment. CurrentDirectory;
A. DisallowBindingRedirects = false;
A. DisallowCodeDownload = true;
A. ConfigurationFile =
AppDomain. CurrentDomain. SetupInformation. ConfigurationFile;
// Create a new application domain

AppDomain B = AppDomain. CreateDomain ("B", null, ads );
Export albyreftype mbrt =
(MarshalByRefType) B. CreateInstanceAndUnwrap (
ExeAssembly,
Typeof (MarshalByRefType). FullName
);
Mbrt. SomeMethod (callingDomainName );
// Close the application domain

AppDomain. Unload (ad2 );
Try
{
// Call the method again. Note that this time it fails
// Because the second AppDomain was unloaded.
Mbrt. SomeMethod (callingDomainName );
Console. WriteLine ("Sucessful call .");
}
Catch (AppDomainUnloadedException)
{
Console. WriteLine ("Failed call; this is expected .");
}
}
}
Public class implements albyreftype: Implements albyrefobject
{

// Call the method through proxy

Public void SomeMethod (string callingDomainName)
{

AppDomainSetup ads = AppDomain. CurrentDomain. SetupInformation;
Console. WriteLine ("AppName = {0}, AppBase = {1}, ConfigFile = {2 }",
A. ApplicationName,
A. ApplicationBase,
A. ConfigurationFile
);
Console. WriteLine ("Calling from '{0}' to '{1}'.", callingDomainName,

Thread. GetDomain (). FriendlName );
}
}

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.