. Net application domain

Source: Internet
Author: User

AppDomain is a logical container of a set of assemblies. The unique function of AppDomain is to isolate. It is unique in. Net. When creating a hosted application, a default AppDomain will be created at the same time.

Specific AppDomain Functions

1. The object created by the code in one AppDomain cannot be directly accessed by the code in another AppDomain

If you need access, you need to use a proxy, that is, use "by reference to mail" and "by value mail"

2. AppDomain can be uninstalled

CLR does not support uninstalling an assembly from an AppDomain. However, it can uninstall the AppDomain and release all the resources allocated in the AppDomain.

3. AppDomain can be separately protected

You can assign a permission when creating an AppDomain.

4. AppDomain can be configured separately

You can also configure the AppDomain independently when creating an AppDomain.

Create AppDomain

// Parameter description: 1: Application domain name 2: Permission 3: Pass null indicates that the current AppDomain is configured
AppDomain appDomain = AppDomain. CreateDomain ("application domain name", null, null );

Cross-AppDomain Access Object

There are three types of actions for cross-AppDomain access objects: "send by reference", "Send by value", and "cannot send by value"

First, construct three classes: "mail by application", "mail by value", and "cannot mail". note how to mail, or whether to mail, it is determined by this type.

 

// Mail by Application
Public class implements labyreftype: Implements albyrefobject
{
Public void DoSomthing ()
{
//.....
}
}

// Send messages by value
[Serializable]
Public class externalbyvaltype: Object
{
Public void DoSomthing ()
{
//.....
}
}

// Cannot be sent
Public class non‑alabletype: Object
{
Public void DoSomthing ()
{
//.....
}
}

Call:

// Obtain the current AppDomain through the thread
AppDomain currAppDomain = System. Threading. Thread. GetDomain ();
// Output the current AppDomain name. The FriendlyName is read-only and can be assigned only when it is created. The default AppDomain name is the application name.
Console. WriteLine (currAppDomain. FriendlyName );

// Obtain the name of the current Assembly
String exeAssemble = Assembly. GetEntryAssembly (). FullName;

Send by reference

// Mail by Application
AppDomain refAppDomain = AppDomain. CreateDomain ("RefAddDomain", null, null );
Export albyreftype into alreftype = (MarshalByRefType) refAppDomain. CreateInstanceAndUnwrap (exeAssemble, "MarshalByRefType ");
/*
* Smooth execution
* When the code is executed here, the thread will switch to the refAppDomain application and execute the real DoSomthring ()
* The refAppDomain here is only a proxy reference
*/
Externalreftype. DoSomthing ();
// Call IsTransparentProxy to determine whether the Result is a proxy Result: true
Console. WriteLine ("Is Proxy = {0}", RemotingServices. IsTransparentProxy (refAppDomain ));

Send by value

// Send messages by value
AppDomain valAppDomain = AppDomain. CreateDomain ("RefAddDomain", null, null );
Export albyvaltype into alvaltype = (MarshalByValType) valAppDomain. CreateInstanceAndUnwrap (exeAssemble, "MarshalByValType ");
/*
* Smooth execution
* When the code is executed here, the DoSomthing () method of the current AppDomain implements alvaltype is actually called.
* Because MarshalByValType marks the Serializable feature
*/
Externalreftype. DoSomthing ();
// Call IsTransparentProxy to determine whether it is a proxy Result: false
Console. WriteLine ("Is Proxy = {0}", RemotingServices. IsTransparentProxy (financialvaltype ));

Cannot be sent

// Cannot be sent
AppDomain nonAppDomain = AppDomain. CreateDomain ("RefAddDomain", null, null );
Non‑alabletype externalnontype = (non‑alabletype) nonAppDomain. CreateInstanceAndUnwrap (exeAssemble, "non‑alabletype ");
/*
* Exception
*/
Externalreftype. DoSomthing ();

 

Monitor AppDomain
Set the MonitoringEnabled attribute of AppDomain to true. If monitoring is enabled and set to true, it cannot be set to false. That is, it cannot be disabled after monitoring is enabled.

After monitoring is enabled, you can view the AppDomain running parameters through the following four read-only attributes:

AppDomain. MonitoringIsEnabled = true;
// The number of bytes used by all AppDomains
AppDomain. monitoring1_vedprocessmemorysize;
// The number of bytes currently in use
NonAppDomain. monitoring1_vedmemorysize;
// The direct number allocated by this AppDomain
NonAppDomain. MonitoringTotalAllocatedMemorySize;
// CPU usage
NonAppDomain. MonitoringTotalProcessorTime;

 

AppDomain FirstChance exception notification

By setting the FirstChanceException event, you can obtain the AppDomain exception but cannot terminate it. When the AppDomain encounters an exception, it will search for Catch quickly. If no exception is found, it will continue to throw, the program is aborted due to an exception.

 

Note:

Different AppDomain references System. dll, each AppDomain will create its own memory area, that is to say, the AppDomain view is irrelevant, and each operation is done independently. Although this is a waste, it is necessary to ensure isolation, when the AppDomain is uninstalled, other AppDomains are not affected.

 

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.