C # to strengthen the series of articles VI: Application Domain (AppDomain) Analysis

In the traditional development before we all know, an application corresponds to a process, and specify the virtual memory for the process by the operating system to map the actual physical memory, the effective maintenance of security between processes. On the other hand, each process will consume some system resources, reducing performance, and inter-process communication too much trouble.
It introduced a new concept in .Net: The application domain (AppDomain). Can be understood as many application domains can run on the same .NET process, the system can reduce consumption, while isolated from each other between different domains, guaranteed in terms of safety. In addition to a process in the same communication between different domains are also comparatively easy.
The contents of the application domain involves many, this paper briefly describes the following two aspects:
1, how to create, uninstall domain
2, how to realize the communication between the domain

a, how to create, uninstall domain
provides AppDomain class in .NET to implement the hosting Code to provide isolation, unloading, and security boundaries.

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, using the properties of the new class definition AppDomainSetup domains, such as may be provided in the root directory of the application, setting the category of the loaded program.
Used in the examples are SingleDomain represent loader must not share internal resources across application domains, you can also use MultiDomain, MultiDomainHost other properties such as
2, in the fourth line to create a name for the new domain MyDomain
3, in the 5th row the implementation of a new application within the domain
4, line 6 to uninstall the new domain
after the adoption of this creation, implementation of the new system abnormality occurs even if the domain does not affect the implementation of the original domain, then you could do something like WatchDog (routine monitoring, Once the exit to restart) program of the

Second, how to implement the communication between domains

common language runtime prohibit direct calls between objects in different domains, but we can replicate these objects, or access them 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);

Start code are similar, focusing on the following aspects:
1, to create an object (class DomainCom.TestStatic) 5 line the new domain, and return a proxy class ObjectHandle for passing objects between multiple application domains
DomainCom.TestStatic must inherit from the MarshalByRefObject class, in order to facilitate demonstration, this class is very simple, interface inherits from ICollection, on the realization of a Count property:

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;
            }

        }


        
#region 未实现代码
        
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、第6行取得新域中的对象
3、在第七在当前域中给新域中的对象赋值
4、第8行执行新域中的应用程序,这个应用程序中就是弹出一个对话框显示Count的值

            TestStatic test  =   new  TestStatic();
            MessageBox.Show(test.Count.ToString());

得到的结果为4,证明实现了域间对象的互操作,这样我们就可以实现其他更复杂的操作了。

引用自 :http://www.cnblogs.com/firstyi/archive/2008/03/14/1106068.html

转载于:https://www.cnblogs.com/zhangchenliang/archive/2012/08/01/2618324.html

Guess you like

Origin blog.csdn.net/weixin_34259159/article/details/93495145