Application domain
1. What is an application domain?
Application domain (AppDomain) is a boundary that is built by the Common Language Runtime library around the objects created within the same application range (that is, starting from the application entry point, any location along the sequence of object activation ). The application domain helps isolate the objects created in one application from those created in other applications for predictable runtime behavior. Multiple application domains can exist in a single process. For details, refer to Baidu Encyclopedia: http://baike.baidu.com/view/159546.htm? Fr = aladdin
2. When to use the application domain?
Errors in one application do not affect other applications. Because the type-safe code does not cause memory errors, you can use the application domain to ensure that the code running in one domain does not affect other applications in the process. A single application can be stopped without stopping the entire process. The application domain allows you to uninstall code running in a single application. Note that you cannot uninstall a single assembly or type. Only the entire domain can be detached.
3. Simple examples of application domains?
SetupInfo
1 [Serializable] 2 public class SetupInfo: externalbyrefobject 3 {4 // <summary> Application domain id </summary> 5 public string Id {get; set ;} 6 7 // <summary> Application directory </summary> 8 public string BinaryDirectory {get; set ;} 9 10 /// <summary> application parent directory </summary> 11 public string BaseDirectory {get; set ;} 12 13 // <summary> Application entry point </summary> 14 public string EntryPoint {get; set;} 15}
View Code
ProxyObject proxy object
1 [Serializable] 2 public class ProxyObject: externalbyrefobject 3 {4 public Assembly assembly; 5 6 public object instance; 7 8 public Type currentType; 9 10 public void Initialize () 11 {12 var setupInfo = AppDomain. currentDomain. getData ("SETUPINFO") as SetupInfo; 13 14 string entryClass = "ZLP. AWSW. person "; 15 string assemblyName =" ZLP. AWSW "+ ". dll "; 16 string assemblyPath = Path. combine (setupInfo. BinaryDirectory, assemblyName); 17 if (! File. exists (assemblyPath) 18 {19 return; 20} 21 assembly = Assembly. loadFrom (assemblyPath); 22 instance = assembly. createInstance (entryClass); 23 currentType = instance. getType (); 24 var methinfo = currentType. getMethod ("Execute"); 25 methinfo. invoke (instance, null); 26} 27}
View Code
Proxy
1 [Serializable] 2 public class Proxy: externalbyrefobject 3 {4 public Proxy (SetupInfo setupInfo) 5 {6 this. setupInfo = setupInfo; 7 this. id = setupInfo. id; 8} 9 10 public string Id {get; set;} 11 12 public AppDomain Domain {get; set;} 13 14 public ProxyObject {get; set ;} 15 16 public SetupInfo {get; set;} 17 18 public void Start () 19 {20 if (Domain = null) 21 {22 Domain = CreateDomain (); 23} 24 if (ProxyObject = null) 25 {26 ProxyObject = CreateProxyObject (); 27} 28} 29 30 public AppDomain CreateDomain () 31 {32 var setup = new AppDomainSetup (); 33 34 var cachePath = AppDomain. currentDomain. setupInformation. cachePath; 35 var configurationFile = Path. combine (SetupInfo. binaryDirectory, "app. config "); 36 setup. applicationBase = SetupInfo. baseDirectory; 37 setup. privateBinPath = SetupInfo. binaryDirectory; 38 setup. shadowCopyFiles = "true"; 39 setup. applicationName = SetupInfo. id; 40 setup. shadowCopyDirectories = string. format ("{0}; {1}", SetupInfo. baseDirectory, SetupInfo. binaryDirectory); 41 setup. cachePath = cachePath; 42 setup. loaderOptimization = LoaderOptimization. multiDomainHost; 43 if (File. exists (configurationFile) 44 {45 setup. configurationFile = configurationFile; 46} 47 AppDomain domain = AppDomain. createDomain (setup. applicationName, AppDomain. currentDomain. evidence, setup); 48 domain. doCallBack (new CrossAppDomainDelegate (delegate49 {50 LifetimeServices. leaseTime = TimeSpan. zero; 51}); 52 domain. setData ("SETUPINFO", SetupInfo); 53 domain. domainUnload + = new EventHandler (DomainUnload); 54 domain. unhandledException + = new UnhandledExceptionEventHandler (UnhandledException); 55 return domain; 56} 57 58 public ProxyObject CreateProxyObject () 59 {60 Type type = typeof (ProxyObject); 61 Assembly currentAssembly = Assembly. getExecutingAssembly (); 62 ProxyObject proxyObject = Domain. createInstanceAndUnwrap (currentAssembly. fullName, type. fullName) as ProxyObject; 63 currentAssembly = null; 64 type = null; 65 return proxyObject; 66} 67 68 private void UnhandledException (object sender, UnhandledExceptionEventArgs e) 69 {70 71} 72 73 private void DomainUnload (object sender, EventArgs e) 74 {75 76} 77}
View Code
The above is for reference only ....
Application domain