C # application domain

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.

A new concept is introduced in. NET: C # 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); 

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 indicates that the loader cannot share internal resources between C # application domains, and other attributes such as MultiDomain and MultiDomainHost can be used.

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. AppDomainSetupinfo2=newAppDomainSetup();  
  2. info2.LoaderOptimization=LoaderOptimization.SingleDomain;  
  3. info2.ApplicationBase="C:\test";  
  4. AppDomainAppDomaindomain2=AppDomain.CreateDomain("MyDomain2",null,info2);  
  5. ObjectHandleobjHandle=domain2.CreateInstance
    ("DomainCom","DomainCom.TestStatic");  
  6. ICollectionobj=objHandle.Unwrap()asICollection;  
  7. inti=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 C # 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:

 
 
  1. UsingSystem;
  2. UsingSystem. Collections. Generic;
  3. UsingSystem. Text;
  4. UsingSystem. Collections;
  5.  
  6. NamespaceDomainCom
  7. {
  8. PublicclassTestStatic: financialbyrefobject, ICollection
  9. {
  10. Privatestaticintcount = 1;
  11.  
  12. PublicintCount
  13. {
  14. Get
  15. {
  16. Countcount = count * 2;
  17. Returncount;
  18. }
  19. }
  20.  
  21. Unimplemented Code # region unimplemented code
  22. PublicboolIsSynchronized {get {thrownewNotImplementedException ();}}
  23.  
  24. PublicobjectSyncRoot {get {thrownewNotImplementedException ();}}
  25.  
  26. PublicvoidCopyTo (Arrayarray, intindex)
  27. {
  28. ThrownewNotImplementedException ();
  29. }
  30. PublicIEnumeratorGetEnumerator ()
  31. {
  32. ThrownewNotImplementedException ();
  33. }
  34. # Endregion
  35. }
  36. }

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.

Related Article

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.