Application domain analysis

Source: Internet
Author: User
In traditional development, we all know that an application corresponds to a process and specifies virtual memory for the process. The operating system maps the actual physical memory, it effectively maintains 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 provides isolation, uninstallation, and security boundaries for the execution of managed code. 1 AppDomainSetup info = new AppDomainSetup ();
2 info. LoaderOptimization = LoaderOptimization. SingleDomain;
3
4 AppDomain domain = 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 exampleSingleDomainIt indicates that the loader cannot share internal resources between application domains. You can also useMultiDomain,MultiDomainHostOther 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: MarshalByRefObject, ICollection
{
Private static int count = 1;

Public int Count
{
Get
{
Count = count * 2;
Return count;
}
}

Unimplemented Code # region unimplemented code
Public bool IsSynchronized {get {throw new NotImplementedException ();}}

Public object SyncRoot {get {throw new NotImplementedException ();}}

Public void CopyTo (Array array, int index)
{
Throw new NotImplementedException ();
}
Public IEnumerator GetEnumerator ()
{
Throw new NotImplementedException ();
}
# 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 = new TestStatic ();
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.