C # enhanced series Article 6: application domain (appdomain) Analysis

Source: Internet
Author: User
In the past, we all know that an application Program Corresponds to a process and specifies virtual memory for the process. The operating system maps the actual physical memory to effectively maintain the security between processes. On the other hand, every process consumes a certain amount of system resources, reduces performance, and makes communication between processes troublesome.
In. Net, a new concept is introduced: application domain (appdomain ). It can be understood that many application domains can run in the same. Net process, which can reduce system consumption and isolate different domains to ensure security. In addition, communication between different domains in the same process is relatively simple.
The application domain involves a lot of content. This article briefly describes the following two aspects:
1. How to create and uninstall a domain
2. How to Implement Inter-Domain Communication

1. How to create and uninstall a domain
In. net, the appdomain class is provided for execution hosting. Code Provides isolation, uninstallation, and security boundaries. 1 Appdomainsetup info =   New Appdomainsetup ();
2 Info. loaderoptimization = Loaderoptimization. singledomain;
3
4 Appdomain = Appdomain. createdomain ( " Mydomain " , Null , Info );
5 Domain. executeassembly ( " C: \ test \ domaincom.exe " );
6 Appdomain. Unload (domain );
7

1. Use the appdomainsetup class to define attributes of the new domain. For example, you can set the root directory of the application and the category of the loaded program.
In this example Singledomain It indicates that the loader cannot share internal resources between application domains. You can also use Multidomain, Multidomainhost Other attributes
2. Create a new domain named mydomain in row 4.
3. Execute an application in the new domain in row 5th.
4. unmount the new domain from row 3
After the new domain is created, even if a system exception occurs, the execution of the original domain will not be affected. Then, you can run a program similar to watchdog (Monitoring subroutine, which will be restarted once it exits ).

2. How to Implement Inter-Domain Communication

The Common Language Runtime Library prohibits direct calls between objects in different domains, but we can copy these objects or access these objects through a proxy. 1 Appdomainsetup info2 =   New Appdomainsetup ();
2 Info2.loaderoptimization = Loaderoptimization. singledomain;
3 Info2.applicationbase =   " C: \ test " ;
4 Appdomain domain2 = Appdomain. createdomain ( " Mydomain2 " , Null , Info2 );
5 Objecthandle objhandle = Domain2.createinstance ( " Domaincom " , " Domaincom. teststatic " );
6 Icollection OBJ = Objhandle. Unwrap () As Icollection;
7 Int I = OBJ. count;
8 Domain2.executeassembly ( " C: \ test \ domaincom.exe " );
9 Appdomain. Unload (domain2 );

The initial code is similar, with the focus on the following aspects:
1. Create an object (class domaincom. teststatic) in the new domain in row 5th, and return a proxy objecthandle class for passing objects between multiple application domains
Domaincom. teststatic must be inherited from the marshalbyrefobject class. For demonstration convenience, this class is very simple. inherited from the icollection interface, a Count attribute is implemented: Using System;
Using System. Collections. Generic;
Using System. text;
Using System. collections;

Namespace Domaincom
{
Public   Class Teststatic: externalbyrefobject, icollection
{
Private   Static   Int Count =   1 ;

Public   Int Count
{
Get
{
Count=Count* 2;
ReturnCount;
}
}

Unimplemented code # Region Unimplemented code
Public   Bool Issynchronized {Get {Throw NewNotimplementedexception ();}}

Public   Object Syncroot {Get {Throw NewNotimplementedexception ();}}

Public   Void Copyto (array, Int Index)
{
Throw NewNotimplementedexception ();
}
Public Ienumerator getenumerator ()
{
Throw NewNotimplementedexception ();
}
# Endregion
}
}

2. Row 3 retrieves objects in the new domain
3. assign values to objects in the new domain in the seventh region.
4. Execute the application in the new domain in row 8th. In this application, a dialog box is displayed, showing the Count value. Teststatic Test= NewTeststatic ();
MessageBox. Show (test. Count. tostring ());

The result is4,It proves that the inter-domain object interoperability is realized, so that we can implement other more complex operations.

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.